Skip to content

Instantly share code, notes, and snippets.

@masterPiece93
Last active September 2, 2025 08:28
Show Gist options
  • Save masterPiece93/9515a7d14fa1a06a82139840952c7ecb to your computer and use it in GitHub Desktop.
Save masterPiece93/9515a7d14fa1a06a82139840952c7ecb to your computer and use it in GitHub Desktop.
NATS

NATS

| Go | python |

Configuring NATS via Docker

Simply follow the steps below :

fetch image :

docker pull nats:latest

executing :

docker run -p 4222:4222 -ti nats:latest

executing the JetStream :

docker run -p 4222:4222 -ti nats:latest -js

link to study

Verify/Test Docker Nats Server through code :

  1. via GoLang code
  2. via Python code

NOTE : The sample scripts in the above examples are a good starting point on understanding NATS from developer standpoint


GoLang

pre-requisite :

  • Create Go Module :

    go mod init telus.com/nats/goLang
  • Create Go Package Dir :

    mkdir nats-pub
    mkdir nats-sub
  • Get Example/Test Scripts :

    publisher script :

    cd nats-pub
    curl -o main.go -O -L https://raw.githubusercontent.com/nats-io/nats.go/refs/heads/main/examples/nats-pub/main.go
    # go back on root
    cd ..

    subscriber script :

    cd nats-sub
    curl -o main.go -O -L https://raw.githubusercontent.com/nats-io/nats.go/refs/heads/main/examples/nats-sub/main.go
    # go back on root
    cd ..
  • get all requirements

    go mod tidy

Dir Structure that we need to aechieve :

.
├── go.mod
├── go.sum
├── how_to_test.md
├── nats-pub
│   └── main.go
└── nats-sub
    └── main.go

NOTE : . is the root

run Subscriber :

  • on a new teminal

    run :

    go run nats-sub/main.go <subject-name>

run Publisher :

  • on a new teminal

    run :

    go run nats-pub/main.go <subject-name> <message>


Target : you recieve every message you send from the publisher terminal , into Subscriber terminal


Python

pre-requisite :

  • Create Virtual Environment :

    python3 -m venv venv
  • Get Example/Test Scripts :

    publisher script :

    curl -o nats-pub.py -O -L https://raw.githubusercontent.com/nats-io/nats.py/refs/heads/main/examples/nats-pub/__main__.py
    

    subscriber script :

    curl -o nats-pub.py -O -L https://raw.githubusercontent.com/nats-io/nats.py/refs/heads/main/examples/nats-sub/__main__.py
    

run Subscriber :

  • on a new teminal

    activate virtual environment :

    source venv/bin/activate

    run :

    python3 nats-sub.py

run Publisher :

  • on a new teminal

    activate virtual environment :

    source venv/bin/activate
    1. for publishing a single message :

      python3 nats-pub.py
    2. for publishing a multiple messages :

      python3 -c 'import os;[os.system("python nats-pub.py") for _ in range(10)]'

Installing Nats-Cli Tools

Official releases

download a suitable package from above release artifactory .

  • For Ubuntu : nats-0.1.6-amd64.deb one worked for me :asOn-2025-jan-15

    • you may choose according to your system

Nats-Cli In Publisher-Subscriber System

NOTE : Make sure NATS Server is running

NOTE : Make sure Nats-Cli is connected to NATS Server

Publishing Message

Using publish command : nats publish [<flags>] <subject> [<body>]
flags
  • details of each flag :

    Flags:
        --reply=REPLY        Sets a custom reply to subject 
    -H, --header=HEADER ...  Adds headers to the message
        --count=1            Publish multiple messages
        --sleep=SLEEP        When publishing multiple messages,
                            sleep between publishes
        --force-stdin        Force reading from stdin
    
    Global Flags:
    -h, --help                    Show context-sensitive help
        --version                 Show application version.
    -s, --server=URL              NATS server urls ($NATS_URL)
        --user=USER               Username or Token
                                    ($NATS_USER)
        --password=PASSWORD       Password ($NATS_PASSWORD)
        --connection-name=NAME    Nickname to use for the
                                    underlying NATS Connection
        --creds=FILE              User credentials
                                    ($NATS_CREDS)
        --nkey=FILE               User NKEY ($NATS_NKEY)
        --tlscert=FILE            TLS public certificate
                                    ($NATS_CERT)
        --tlskey=FILE             TLS private key ($NATS_KEY)
        --tlsca=FILE              TLS certificate authority
                                    chain ($NATS_CA)
        --[no-]tlsfirst           Perform TLS handshake before
                                    expecting the server greeting
        --timeout=DURATION        Time to wait on responses
                                    from NATS ($NATS_TIMEOUT)
        --socks-proxy=PROXY       SOCKS5 proxy for
                                    connecting to NATS server
                                    ($NATS_SOCKS_PROXY)
        --js-api-prefix=PREFIX    Subject prefix for access to
                                    JetStream API
        --js-event-prefix=PREFIX  Subject prefix for access to
                                    JetStream Advisories
        --js-domain=DOMAIN        JetStream domain to access
        --inbox-prefix=PREFIX     Custom inbox prefix to use
                                    for inboxes
        --colors=SCHEME           Sets a color scheme to use
                                    ($NATS_COLOR)
        --context=NAME            Configuration context
                                    ($NATS_CONTEXT)
        --trace                   Trace API interactions
        --no-context              Disable the selected context
    

body


Examples

In a terminal/cmd/powershell , start a subscriber for testing all the examples
# your subscriber window
nats sub "intros" 

  1. Publishing simple message

    . 1 .

    In your terminal / cmd , Run the following :
    • publisher
      # for publishing a message : nats publish <subject> <message>
      nats pub "intros" "Hello from Foo"
    • check output in your subscriber window

  1. Publishing simple message with flags

    . 1 .

    we will use following flags :

    | --count |

    In your terminal / cmd , Run the following :
    • publisher
      # for publishing a message : nats publish <subject> <message>
      nats pub --count 10 "intros" "Hello from Foo"
    • check output in your subscriber window

    . 2 .

    we will use following flags :

    | --count | --sleep |

    In your terminal / cmd , Run the following :
    • publisher
      # for publishing a message : nats publish <subject> <message>
      nats pub --count 10 --sleep=500ms "intros" "Hello from Foo"
    • check output in your subscriber window

    . 3 .

    we will use following flags :

    | --count | --sleep | -H , --header |

    In your terminal / cmd , Run the following :
    • publisher
      # for publishing a message : nats publish <subject> <message>
      nats pub --count 10 --sleep=500ms "intros" "message - {{Count}}" -H Header1:One -H Header2:Two 
    • check output in your subscriber window

    . 4 .

    we will use following flags :

    | --count | --sleep | -H , --reply |

    --reply header is used in request-reply system on NATS


  1. Publishing simple message with template functions

    It supports GoLang templating . Following template functions are supported in a message string :

    Count the message number
    TimeStamp RFC3339 format current time
    Unix seconds since 1970 in UTC
    UnixNano nano seconds since 1970 in UTC
    Time the current time
    ID an unique IDID
    Random(min, max) random string at least min long, at most max

    . 1 .

    we will use following flags :

    | --count | --sleep |

    we will use the following template functions :

    | Count |

    • publisher
      # command for publishing a message with counts 
      nats pub "intros" "Hello from Foo - Msg. No. #{{Count}}" --count 10
    • check output in your subscriber window

    . 2 .

    we will use following flags :

    | --count | --sleep |

    we will use the following template functions :

    | Count |

    • publisher
      # command for publishing a message with counts
      nats pub "intros" "Hello from Foo - Msg. No. {{Count}}" --count 10 --sleep=1s
    • check output in your subscriber window

    . 4 .

    we will use following flags :

    | --count | --sleep |

    we will use the following template functions :

    | Count | Time |

    In your terminal / cmd , Run the following :
    • publisher
      # command for publishing a message with counts and time .
      nats pub "intros" "Hello from Foo - Msg. No. {{Count}} at {{Time}}" --count 10 --sleep=1s
    • check output in your subscriber window

    . 5 .

    we will use following flags :

    | --count | --sleep |

    we will use the following template functions :

    | Count | Time | Random |

    In your terminal / cmd , Run the following :
    • publisher
      # command for publishing a message with counts .
      nats pub "intros" "{{Count}} at {{Time}} . Hash : {{Random 10 100}}" --count 10 --sleep=1s
    • check output in your subscriber window

    . 6 .

    we will use following flags :

    | --count | --sleep |

    we will use the following template functions :

    | Count | Timestamp |

    In your terminal / cmd , Run the following :
    • publisher
      # run command :
      nats pub "intros" "{{Count}} at {{TimeStamp}}" --count 10 --sleep=1s
    • check output in your subscriber window

    . 7 .

    we will use following flags :

    | --count , --sleep |

    we will use the following template functions :

    | Template Functions | ID , TimeStamp |

    In your terminal / cmd , Run the following :
    • publisher
      # run command :
      nats pub "intros" "Id : {{ID}} at {{TimeStamp}}" --count 10 --sleep=1s
    • check output in your subscriber window
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment