| Go
| python
|
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
NOTE : The sample scripts in the above examples are a good starting point on understanding NATS from developer standpoint
-
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
-
on a new teminal
run :
go run nats-sub/main.go <subject-name>
-
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
-
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
-
on a new teminal
activate virtual environment :
source venv/bin/activate
run :
python3 nats-sub.py
-
on a new teminal
activate virtual environment :
source venv/bin/activate
-
for publishing a single message :
python3 nats-pub.py
-
for publishing a multiple messages :
python3 -c 'import os;[os.system("python nats-pub.py") for _ in range(10)]'
-
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
-
NOTE : Make sure NATS Server is running
NOTE : Make sure Nats-Cli is connected to NATS Server
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
# your subscriber window
nats sub "intros"
-
Publishing simple message
. 1 .
- publisher
# for publishing a message : nats publish <subject> <message> nats pub "intros" "Hello from Foo"
- check output in your subscriber window
- publisher
-
Publishing simple message with flags
. 1 .
|
--count
|- publisher
# for publishing a message : nats publish <subject> <message> nats pub --count 10 "intros" "Hello from Foo"
- check output in your subscriber window
. 2 .
|
--count
|--sleep
|- 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 .
|
--count
|--sleep
|-H
,--header
|- 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 .
|
--count
|--sleep
|-H
,--reply
|--reply
header is used in request-reply system on NATS - publisher
-
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 .
|
--count
|--sleep
||
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 .
|
--count
|--sleep
||
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 .
|
--count
|--sleep
||
Count
|Time
|- 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 .
|
--count
|--sleep
||
Count
|Time
|Random
|- 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 .
|
--count
|--sleep
||
Count
|Timestamp
|- publisher
# run command : nats pub "intros" "{{Count}} at {{TimeStamp}}" --count 10 --sleep=1s
- check output in your subscriber window
. 7 .
|
--count
,--sleep
|| Template Functions |
ID
,TimeStamp
|- publisher
# run command : nats pub "intros" "Id : {{ID}} at {{TimeStamp}}" --count 10 --sleep=1s
- check output in your subscriber window
- publisher