Nats Jet Strem, Hello World

Download and install from the release form this web site.

bash
go get github.com/nats-io/nats-server/v2

for install Nats-cli follow below link:

text
https://github.com/nats-io/natscli

Run Nats server

bash
nats server run
text
[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 ready

Select the Context

bash
nats context select nats_development
text
NATS 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.json

Mechanism Request Reply

Reply data from the topic

bash
nats reply hello.jermy "hi"
text
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

bash
nats req hello.jermy ""
text
11:22:44 Sending request on "hello.jermy"11:22:44 Received with rtt 403.218µshi
bash
nats req hello.jermy "{ "hi" : "bye" }"11:26:33 Sending request on "hello.jermy"11:26:33 Received
with rtt 399.847µshi
text
Recive :11:26:33 [#6] Received on subject "hello.jermy":{ hi : bye }

Mechanism Pub Sub

sub receive data from the topic

bash
nats sub hello.world[#2] Received on "hello.world"hi?

Publish data to the topic

bash
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

bash
nats sub hello.world[#483] Received on "hello.world"hi, guys[#484] Received on "hello.world"group 2

Publish data to the topic

bash
nats pub hello.world "hi, guys" --count=-1 --sleep=1s
bash
nats pub hello.world "group 2" --count=-1 --sleep=1s

Mechanism 1 Pub and many Sub

Fan out Design pattern

Nats Jet Strem, Hello World

2 subs receive data from the topic

bash
nats sub hello.world04:32:15 Subscribing on hello.world [#1] Received on "hello.world"message
bash
nats sub hello.world04:33:03 Subscribing on hello.world [#1] Received on "hello.world"message

Publish data to the topic

bash
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

bash
nats sub hello.world --queue greeter04:34:26 Subscribing on hello.world [#1] Received on
"hello.world"message
bash
nats sub hello.world --queue greeter04:34:42 Subscribing on hello.world

Publish data to the topic

bash
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.

bash
nats pub hello.world "message" --count 10001000 / 1000
[========================================================================================================================================]    0s
text
[#501] Received on "hello.world"message
text
[#499] Received on "hello.world"message[#500] Received on "hello.world"message

Nats JetsStream

bash
nats server run --jetstream

Context selected

bash
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.json

List of Nats Jetstream exist

bash
nats stream lsNo Streams defined

Watch Nats Stream lists

text
watch nats stream ls

Make Nats Stream

bash
nats stream add
text
? 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
Nats Jet Strem, Hello World

send Message into orders

bash
nats pub orders.us "{{.Count}}" --count 1000010000 / 10000
[========================================================================================================================================]    1s
Nats Jet Strem, Hello World

Read from stream

bash
nats sub --stream orders
text
[#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:301000

Reading all messages from the topics again

bash
nats sub --stream orders --all

Reading new messages from the topic

bash
nats sub --stream orders --new05:08:21 Subscribing to JetStream Stream holding messages with subject
orders.* delivering any new messages received

Reading only last message

bash
nats sub --stream orders --last
text
05: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:301000

Get last message from specific subject

bash
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:301000

Get messages from specific sequence

bash
nats sub --stream orders --start-sequence=21900

Reading messages and clean up the queue

bash
nats sub  "orders.*" --durable my_consumer

Create Consumer

bash
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:30

Pull messages

bash
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 message

Check consumer

bash
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 512

Nats Key value store

bash
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 delete

Create key value buket

bash
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: File

Set key and value

bash
nats kv put one hello world
bash
nats kv watch one[2022-12-28 05:33:17] PUT one > hello: world
Nats Jet Strem, Hello World

mix stream with key value store

Nats Jet Strem, Hello World

Delete the stream and see the list of data in stream

text
watch nats stream lsnats stream purge NAMEOFSTREAMnats sub --stream CALLBACKS

Limits-based Stream in JetStream

Delivery reliability