Nats Jet Strem, Hello World
Download and install from the release form this web site.
go get github.com/nats-io/nats-server/v2for install Nats-cli follow below link:
https://github.com/nats-io/natscliRun Nats server
nats server run[54726] [INF] Starting nats-server[54726] [INF] Version: 2.9.6[54726] [INF] Git: [not
set][54726] [INF] Name: nats_development[54726] [INF] ID:
NCT7AV2WQ5UUD3EXWS3WJ6RBFP5GRQQ7D6UWK345VGXDQ24PQI3PPN4O[54726] [INF] Using configuration file:
/tmp/nats-server-run-1900677043.cfg[54726] [INF] Listening for client connections on
0.0.0.0:36275[54726] [INF] Server is readySelect the Context
nats context select nats_developmentNATS Configuration Context "nats_development" Description: Local user access for NATS
Development instance Server URLs: nats://0.0.0.0:36275 Username: local
Password: ********* Path: /home/usr/.config/nats/context/nats_development.jsonMechanism Request Reply
Reply data from the topic
nats reply hello.jermy "hi"11:16:55 Listening on "hello.jermy" in group "NATS-RPLY-22"11:22:33 [#0] Received on subject
"hello.jermy":11:22:36 [#1] Received on subject "hello.jermy":11:22:41 [#2] Received on subject
"hello.jermy":11:22:44 [#3] Received on subject "hello.jermy":Request Data into the topic
nats req hello.jermy ""11:22:44 Sending request on "hello.jermy"11:22:44 Received with rtt 403.218µshinats req hello.jermy "{ "hi" : "bye" }"11:26:33 Sending request on "hello.jermy"11:26:33 Received
with rtt 399.847µshiRecive :11:26:33 [#6] Received on subject "hello.jermy":{ hi : bye }Mechanism Pub Sub
sub receive data from the topic
nats sub hello.world[#2] Received on "hello.world"hi?Publish data to the topic
nats pub hello.world "hi?"11:46:50 Published 3 bytes to "hello.world"Mechanism many Pub and 1 Sub
sub receive data from the topic
nats sub hello.world[#483] Received on "hello.world"hi, guys[#484] Received on "hello.world"group 2Publish data to the topic
nats pub hello.world "hi, guys" --count=-1 --sleep=1snats pub hello.world "group 2" --count=-1 --sleep=1sMechanism 1 Pub and many Sub
Fan out Design pattern

2 subs receive data from the topic
nats sub hello.world04:32:15 Subscribing on hello.world [#1] Received on "hello.world"messagenats sub hello.world04:33:03 Subscribing on hello.world [#1] Received on "hello.world"messagePublish data to the topic
nats pub hello.world "message"04:33:20 Published 7 bytes to "hello.world"Mechanism 1 Pub and many Sub with queue
In this Mechanim only one of the sub will get the data
2 subs receive data from the topic
nats sub hello.world --queue greeter04:34:26 Subscribing on hello.world [#1] Received on
"hello.world"messagenats sub hello.world --queue greeter04:34:42 Subscribing on hello.worldPublish data to the topic
nats pub hello.world "message"04:34:56 Published 7 bytes to "hello.world"If sending 1000 messages in this method, we will get as unique distributed in all of each pub services.
nats pub hello.world "message" --count 10001000 / 1000
[========================================================================================================================================] 0s[#501] Received on "hello.world"message[#499] Received on "hello.world"message[#500] Received on "hello.world"messageNats JetsStream
nats server run --jetstreamContext selected
nats context select nats_developmentNATS Configuration Context "nats_development" Description:
Local user access for NATS Development instance Server URLs: nats://0.0.0.0:36275
Username: local Password: ********* Path:
/home/user/.config/nats/context/nats_development.jsonList of Nats Jetstream exist
nats stream lsNo Streams definedWatch Nats Stream lists
watch nats stream lsMake Nats Stream
nats stream add? Stream Name orders? Subjects orders.*? Storage file? Replication 1? Retention Policy Limits?
Discard Policy Old? Stream Messages Limit -1? Per Subject Messages Limit -1? Total Stream Size -1?
Message TTL -1? Max Message Size -1? Duplicate tracking time window 2m0s? Allow message Roll-ups No?
Allow message deletion Yes? Allow purging subjects or the entire stream YesStream orders was created
send Message into orders
nats pub orders.us "{{.Count}}" --count 1000010000 / 10000
[========================================================================================================================================] 1s
Read from stream
nats sub --stream orders[#19999] Received JetStream message: stream: orders seq 19999 / subject: orders.eu / time:
2022-12-28T05:03:48+03:309999[#20000] Received JetStream message: stream: orders seq 20000 /
subject: orders.eu / time: 2022-12-28T05:03:48+03:3010000[#20001] Received JetStream message:
stream: orders seq 20001 / subject: orders.us / time: 2022-12-28T05:05:36+03:301[#21999] Received
JetStream message: stream: orders seq 21999 / subject: orders.eu / time:
2022-12-28T05:05:40+03:30999[#22000] Received JetStream message: stream: orders seq 22000 / subject:
orders.eu / time: 2022-12-28T05:05:40+03:301000Reading all messages from the topics again
nats sub --stream orders --allReading new messages from the topic
nats sub --stream orders --new05:08:21 Subscribing to JetStream Stream holding messages with subject
orders.* delivering any new messages receivedReading only last message
nats sub --stream orders --last05:09:19 Subscribing to JetStream Stream holding messages with subject orders.* starting with the
last message received [#1] Received JetStream message: stream: orders seq 22000 / subject: orders.eu
/ time: 2022-12-28T05:05:40+03:301000Get last message from specific subject
nats sub --stream orders --last-per-subject orders.us05:10:44 Subscribing to JetStream Stream
holding messages with subject orders.us for the last messages for each subject in the Stream [#1]
Received JetStream message: stream: orders seq 21000 / subject: orders.us / time:
2022-12-28T05:05:37+03:301000Get messages from specific sequence
nats sub --stream orders --start-sequence=21900Reading messages and clean up the queue
nats sub "orders.*" --durable my_consumerCreate Consumer
nats consumer create? Consumer name pull_consumer? Delivery target (empty for Pull Consumers) ?
Start policy (all, new, last, subject, 1h, msg sequence) all? Acknowledgement policy explicit?
Replay policy instant? Filter Stream by subject (blank for all) ? Maximum Allowed Deliveries -1?
Maximum Acknowledgements Pending 0? Deliver headers only without bodies No? Add a Retry Backoff
Policy No? Select a Stream ordersInformation for Consumer orders > pull_consumer created
2022-12-28T05:22:57+03:30Pull messages
nats consumer next orders pull_consumer --count=10[05:24:09] subj: orders.us / tries: 1 / cons seq:
1 / str seq: 1 / pending: 21,9991Acknowledged message[05:24:09] subj: orders.us / tries: 1 / cons
seq: 2 / str seq: 2 / pending: 21,9982Acknowledged message[05:24:09] subj: orders.us / tries: 1 /
cons seq: 3 / str seq: 3 / pending: 21,9973Acknowledged message[05:24:09] subj: orders.us / tries: 1
/ cons seq: 4 / str seq: 4 / pending: 21,9964Acknowledged message[05:24:09] subj: orders.us / tries:
1 / cons seq: 5 / str seq: 5 / pending: 21,9955Acknowledged message[05:24:09] subj: orders.us /
tries: 1 / cons seq: 6 / str seq: 6 / pending: 21,9946Acknowledged message[05:24:09] subj: orders.us
/ tries: 1 / cons seq: 7 / str seq: 7 / pending: 21,9937Acknowledged message[05:24:09] subj:
orders.us / tries: 1 / cons seq: 8 / str seq: 8 / pending: 21,9928Acknowledged message[05:24:09]
subj: orders.us / tries: 1 / cons seq: 9 / str seq: 9 / pending: 21,9919Acknowledged
message[05:24:09] subj: orders.us / tries: 1 / cons seq: 10 / str seq: 10 / pending:
21,99010Acknowledged messageCheck consumer
nats consumer info? Select a Stream orders? Select a Consumer pull_consumerInformation for Consumer
orders > pull_consumer created 2022-12-28T05:22:57+03:30Configuration: Durable Name:
pull_consumer Pull Mode: true Deliver Policy: All Ack Policy: Explicit
Ack Wait: 30s Replay Policy: Instant Max Ack Pending: 1,000 Max Waiting Pulls: 512State:
Last Delivered Message: Consumer sequence: 10 Stream sequence: 10 Last delivery: 1m8s ago
Acknowledgment floor: Consumer sequence: 10 Stream sequence: 10 Last Ack: 1m8s ago
Outstanding Acks: 0 out of maximum 1,000 Redelivered Messages: 0 Unprocessed Messages:
21,990 Waiting Pulls: 0 of maximum 512Nats Key value store
nats kverror: a subcommand from the list below is required, use --help for full help including flags
and argumentsusage: nats kv <command> [<args> ...]Interacts with a JetStream based Key-Value
storeThe JetStream Key-Value store uses streams to store key-value pairs for an indefinite period or
a per-bucket configured TTL.Subcommands: kv add Adds a new KV Store Bucket kv put Puts a
value into a key kv get Gets a value for a key kv create Puts a value into a key only if
the key is new or
it's last operation was a delete kv update Updates a key with a new value if the previous value matches the given revision kv del Deletes a key or the entire bucket kv purge Deletes a key from the bucket, clearing history before creating a delete marker kv history Shows the full history for a key kv revert Reverts a value to a previous revision using put kv info View the status of a KV store kv watch Watch the bucket or a specific key for updated kv ls List available buckets or the keys in a bucket kv compact Removes all historic values from the store where the last value is a deleteCreate key value buket
nats kv add oneInformation for Key-Value Store Bucket one created
2022-12-28T05:30:54+03:30Configuration: Bucket Name: one History Kept: 1
Values Stored: 0 Backing Store Kind: JetStream Bucket Size: 0 B Maximum Bucket Size:
unlimited Maximum Value Size: unlimited JetStream Stream: KV_one Storage: FileSet key and value
nats kv put one hello worldnats kv watch one[2022-12-28 05:33:17] PUT one > hello: world
mix stream with key value store

Delete the stream and see the list of data in stream
watch nats stream lsnats stream purge NAMEOFSTREAMnats sub --stream CALLBACKSLimits-based Stream in JetStream
Delivery reliability
