{
  "slug": "Nats-Jet-Strem--Hello-World-d909099c88e4",
  "title": "Nats Jet Strem, Hello World",
  "subtitle": "Run simple Nats Jet Stream example",
  "excerpt": "Run simple Nats Jet Stream example",
  "date": "2022-12-28",
  "tags": [
    "NATS"
  ],
  "readingTime": "6 min",
  "url": "https://medium.com/@mobinshaterian/nats-jet-strem-hello-world-d909099c88e4",
  "hero": "https://cdn-images-1.medium.com/max/800/1*H3kuWrFW58SPbqbe14pltw.png",
  "content": [
    {
      "type": "heading",
      "level": 2,
      "text": "Nats Jet Strem, Hello World"
    },
    {
      "type": "embed",
      "provider": "youtube",
      "url": "https://www.youtube.com/embed/hjXIUPZ7ArM?list=PLgqCaaYodvKY22TpvwlsalIArTmc56W9h"
    },
    {
      "type": "paragraph",
      "html": "Download and install from the release form this web site."
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "go get github.com/nats-io/nats-server/v2"
    },
    {
      "type": "paragraph",
      "html": "for install Nats-cli follow below link:"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "https://github.com/nats-io/natscli"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Run Nats server"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "nats server run"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "[54726] [INF] Starting nats-server[54726] [INF]   Version:  2.9.6[54726] [INF]   Git:      [not\nset][54726] [INF]   Name:     nats_development[54726] [INF]   ID:\nNCT7AV2WQ5UUD3EXWS3WJ6RBFP5GRQQ7D6UWK345VGXDQ24PQI3PPN4O[54726] [INF] Using configuration file:\n/tmp/nats-server-run-1900677043.cfg[54726] [INF] Listening for client connections on\n0.0.0.0:36275[54726] [INF] Server is ready"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Select the Context"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "nats context select nats_development"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "NATS Configuration Context \"nats_development\"      Description: Local user access for NATS\nDevelopment instance      Server URLs: nats://0.0.0.0:36275         Username: local\nPassword: *********             Path: /home/usr/.config/nats/context/nats_development.json"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Mechanism Request Reply"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "Reply data from the topic"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "nats reply hello.jermy \"hi\""
    },
    {
      "type": "code",
      "lang": "text",
      "code": "11:16:55 Listening on \"hello.jermy\" in group \"NATS-RPLY-22\"11:22:33 [#0] Received on subject\n\"hello.jermy\":11:22:36 [#1] Received on subject \"hello.jermy\":11:22:41 [#2] Received on subject\n\"hello.jermy\":11:22:44 [#3] Received on subject \"hello.jermy\":"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "Request Data into the topic"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "nats req hello.jermy \"\""
    },
    {
      "type": "code",
      "lang": "text",
      "code": "11:22:44 Sending request on \"hello.jermy\"11:22:44 Received with rtt 403.218µshi"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "nats req hello.jermy \"{ \"hi\" : \"bye\" }\"11:26:33 Sending request on \"hello.jermy\"11:26:33 Received\nwith rtt 399.847µshi"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "Recive :11:26:33 [#6] Received on subject \"hello.jermy\":{ hi : bye }"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Mechanism Pub Sub"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "sub receive data from the topic"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "nats sub hello.world[#2] Received on \"hello.world\"hi?"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "Publish data to the topic"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "nats pub hello.world \"hi?\"11:46:50 Published 3 bytes to \"hello.world\""
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Mechanism many Pub and 1 Sub"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "sub receive data from the topic"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "nats sub hello.world[#483] Received on \"hello.world\"hi, guys[#484] Received on \"hello.world\"group 2"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "Publish data to the topic"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "nats pub hello.world \"hi, guys\" --count=-1 --sleep=1s"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "nats pub hello.world \"group 2\" --count=-1 --sleep=1s"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Mechanism 1 Pub and many Sub"
    },
    {
      "type": "paragraph",
      "html": "Fan out Design pattern"
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*H3kuWrFW58SPbqbe14pltw.png",
      "alt": "Nats Jet Strem, Hello World",
      "caption": "",
      "width": 987,
      "height": 574
    },
    {
      "type": "embed",
      "provider": "youtube",
      "url": "https://www.youtube.com/embed/8BEwZnUIZfw?feature=oembed"
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*gU1K-YBBdBNivOYSCzHa4w.png",
      "alt": "Nats Jet Strem, Hello World",
      "caption": "",
      "width": 740,
      "height": 574
    },
    {
      "type": "paragraph",
      "html": "2 subs receive data from the topic"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "nats sub hello.world04:32:15 Subscribing on hello.world [#1] Received on \"hello.world\"message"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "nats sub hello.world04:33:03 Subscribing on hello.world [#1] Received on \"hello.world\"message"
    },
    {
      "type": "paragraph",
      "html": "Publish data to the topic"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "nats pub hello.world \"message\"04:33:20 Published 7 bytes to \"hello.world\""
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Mechanism 1 Pub and many Sub with queue"
    },
    {
      "type": "paragraph",
      "html": "In this Mechanim only one of the sub will get the data"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "2 subs receive data from the topic"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "nats sub hello.world --queue greeter04:34:26 Subscribing on hello.world [#1] Received on\n\"hello.world\"message"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "nats sub hello.world --queue greeter04:34:42 Subscribing on hello.world"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "Publish data to the topic"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "nats pub hello.world \"message\"04:34:56 Published 7 bytes to \"hello.world\""
    },
    {
      "type": "paragraph",
      "html": "If sending 1000 messages in this method, we will get as unique distributed in all of each pub services."
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "nats pub hello.world \"message\" --count 10001000 / 1000\n[========================================================================================================================================]    0s"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "[#501] Received on \"hello.world\"message"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "[#499] Received on \"hello.world\"message[#500] Received on \"hello.world\"message"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Nats JetsStream"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "nats server run --jetstream"
    },
    {
      "type": "paragraph",
      "html": "Context selected"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "nats context select nats_developmentNATS Configuration Context \"nats_development\"      Description:\nLocal user access for NATS Development instance      Server URLs: nats://0.0.0.0:36275\nUsername: local         Password: *********             Path:\n/home/user/.config/nats/context/nats_development.json"
    },
    {
      "type": "paragraph",
      "html": "List of Nats Jetstream exist"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "nats stream lsNo Streams defined"
    },
    {
      "type": "paragraph",
      "html": "Watch Nats Stream lists"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "watch nats stream ls"
    },
    {
      "type": "paragraph",
      "html": "Make Nats Stream"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "nats stream add"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "? Stream Name orders? Subjects orders.*? Storage file? Replication 1? Retention Policy Limits?\nDiscard Policy Old? Stream Messages Limit -1? Per Subject Messages Limit -1? Total Stream Size -1?\nMessage TTL -1? Max Message Size -1? Duplicate tracking time window 2m0s? Allow message Roll-ups No?\nAllow message deletion Yes? Allow purging subjects or the entire stream YesStream orders was created"
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*ZNmvyCkaPKSMK9Ce-sCC7A.png",
      "alt": "Nats Jet Strem, Hello World",
      "caption": "",
      "width": 733,
      "height": 202
    },
    {
      "type": "paragraph",
      "html": "send Message into orders"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "nats pub orders.us \"{{.Count}}\" --count 1000010000 / 10000\n[========================================================================================================================================]    1s"
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*Zsc_s-t_XpDDHeywDdcksw.png",
      "alt": "Nats Jet Strem, Hello World",
      "caption": "",
      "width": 738,
      "height": 206
    },
    {
      "type": "paragraph",
      "html": "Read from stream"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "nats sub --stream orders"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "[#19999] Received JetStream message: stream: orders seq 19999 / subject: orders.eu / time:\n2022-12-28T05:03:48+03:309999[#20000] Received JetStream message: stream: orders seq 20000 /\nsubject: orders.eu / time: 2022-12-28T05:03:48+03:3010000[#20001] Received JetStream message:\nstream: orders seq 20001 / subject: orders.us / time: 2022-12-28T05:05:36+03:301[#21999] Received\nJetStream message: stream: orders seq 21999 / subject: orders.eu / time:\n2022-12-28T05:05:40+03:30999[#22000] Received JetStream message: stream: orders seq 22000 / subject:\norders.eu / time: 2022-12-28T05:05:40+03:301000"
    },
    {
      "type": "paragraph",
      "html": "Reading all messages from the topics again"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "nats sub --stream orders --all"
    },
    {
      "type": "paragraph",
      "html": "Reading new messages from the topic"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "nats sub --stream orders --new05:08:21 Subscribing to JetStream Stream holding messages with subject\norders.* delivering any new messages received"
    },
    {
      "type": "paragraph",
      "html": "Reading only last message"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "nats sub --stream orders --last"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "05:09:19 Subscribing to JetStream Stream holding messages with subject orders.* starting with the\nlast message received [#1] Received JetStream message: stream: orders seq 22000 / subject: orders.eu\n/ time: 2022-12-28T05:05:40+03:301000"
    },
    {
      "type": "paragraph",
      "html": "Get last message from specific subject"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "nats sub --stream orders --last-per-subject orders.us05:10:44 Subscribing to JetStream Stream\nholding messages with subject orders.us for the last messages for each subject in the Stream [#1]\nReceived JetStream message: stream: orders seq 21000 / subject: orders.us / time:\n2022-12-28T05:05:37+03:301000"
    },
    {
      "type": "paragraph",
      "html": "Get messages from specific sequence"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "nats sub --stream orders --start-sequence=21900"
    },
    {
      "type": "paragraph",
      "html": "Reading messages and clean up the queue"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "nats sub  \"orders.*\" --durable my_consumer"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Create Consumer"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "nats consumer create? Consumer name pull_consumer? Delivery target (empty for Pull Consumers) ?\nStart policy (all, new, last, subject, 1h, msg sequence) all? Acknowledgement policy explicit?\nReplay policy instant? Filter Stream by subject (blank for all) ? Maximum Allowed Deliveries -1?\nMaximum Acknowledgements Pending 0? Deliver headers only without bodies No? Add a Retry Backoff\nPolicy No? Select a Stream ordersInformation for Consumer orders > pull_consumer created\n2022-12-28T05:22:57+03:30"
    },
    {
      "type": "paragraph",
      "html": "Pull messages"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "nats consumer next orders pull_consumer --count=10[05:24:09] subj: orders.us / tries: 1 / cons seq:\n1 / str seq: 1 / pending: 21,9991Acknowledged message[05:24:09] subj: orders.us / tries: 1 / cons\nseq: 2 / str seq: 2 / pending: 21,9982Acknowledged message[05:24:09] subj: orders.us / tries: 1 /\ncons seq: 3 / str seq: 3 / pending: 21,9973Acknowledged message[05:24:09] subj: orders.us / tries: 1\n/ cons seq: 4 / str seq: 4 / pending: 21,9964Acknowledged message[05:24:09] subj: orders.us / tries:\n1 / cons seq: 5 / str seq: 5 / pending: 21,9955Acknowledged message[05:24:09] subj: orders.us /\ntries: 1 / cons seq: 6 / str seq: 6 / pending: 21,9946Acknowledged message[05:24:09] subj: orders.us\n/ tries: 1 / cons seq: 7 / str seq: 7 / pending: 21,9937Acknowledged message[05:24:09] subj:\norders.us / tries: 1 / cons seq: 8 / str seq: 8 / pending: 21,9928Acknowledged message[05:24:09]\nsubj: orders.us / tries: 1 / cons seq: 9 / str seq: 9 / pending: 21,9919Acknowledged\nmessage[05:24:09] subj: orders.us / tries: 1 / cons seq: 10 / str seq: 10 / pending:\n21,99010Acknowledged message"
    },
    {
      "type": "paragraph",
      "html": "Check consumer"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "nats consumer info? Select a Stream orders? Select a Consumer pull_consumerInformation for Consumer\norders > pull_consumer created 2022-12-28T05:22:57+03:30Configuration:        Durable Name:\npull_consumer           Pull Mode: true      Deliver Policy: All          Ack Policy: Explicit\nAck Wait: 30s       Replay Policy: Instant     Max Ack Pending: 1,000   Max Waiting Pulls: 512State:\nLast Delivered Message: Consumer sequence: 10 Stream sequence: 10 Last delivery: 1m8s ago\nAcknowledgment floor: Consumer sequence: 10 Stream sequence: 10 Last Ack: 1m8s ago\nOutstanding Acks: 0 out of maximum 1,000     Redelivered Messages: 0     Unprocessed Messages:\n21,990            Waiting Pulls: 0 of maximum 512"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Nats Key value store"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "nats kverror: a subcommand from the list below is required, use --help for full help including flags\nand argumentsusage: nats kv <command> [<args> ...]Interacts with a JetStream based Key-Value\nstoreThe JetStream Key-Value store uses streams to store key-value pairs for an indefinite period or\na per-bucket configured TTL.Subcommands:  kv add      Adds a new KV Store Bucket  kv put      Puts a\nvalue into a key  kv get      Gets a value for a key  kv create   Puts a value into a key only if\nthe key is new or\nit'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"
    },
    {
      "type": "paragraph",
      "html": "Create key value buket"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "nats kv add oneInformation for Key-Value Store Bucket one created\n2022-12-28T05:30:54+03:30Configuration:          Bucket Name: one         History Kept: 1\nValues Stored: 0   Backing Store Kind: JetStream          Bucket Size: 0 B  Maximum Bucket Size:\nunlimited   Maximum Value Size: unlimited     JetStream Stream: KV_one              Storage: File"
    },
    {
      "type": "paragraph",
      "html": "Set key and value"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "nats kv put one hello world"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "nats kv watch one[2022-12-28 05:33:17] PUT one > hello: world"
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*8viNJ3Ji2X8YLpgePl7D4Q.png",
      "alt": "Nats Jet Strem, Hello World",
      "caption": "",
      "width": 1479,
      "height": 931
    },
    {
      "type": "paragraph",
      "html": "mix stream with key value store"
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*Bij2diYV3vJmZyZFn0foRA.png",
      "alt": "Nats Jet Strem, Hello World",
      "caption": "",
      "width": 921,
      "height": 400
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Delete the stream and see the list of data in stream"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "watch nats stream lsnats stream purge NAMEOFSTREAMnats sub --stream CALLBACKS"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Limits-based Stream in JetStream"
    },
    {
      "type": "paragraph",
      "html": "Delivery reliability"
    }
  ]
}
