Quick start

Start nats-server:

docker run -p 4222:4222 nats:latest

Check ping - pong working

julia> using NATS
julia> nc = NATS.connect()NATS.Connection(unnamed cluster, CONNECTED, 0 subs, 0 unsubs)
julia> @time NATS.ping(nc) # First one may be slow due to compilation 0.100275 seconds (56.09 k allocations: 3.987 MiB, 99.62% compilation time) NATS.Pong()
julia> @time NATS.ping(nc) 0.000480 seconds (37 allocations: 66.781 KiB) NATS.Pong()

Publish subscribe

julia> using NATS
julia> nc = NATS.connect()NATS.Connection(unnamed cluster, CONNECTED, 0 subs, 0 unsubs)
julia> sub = subscribe(nc, "test_subject") do msg @show payload(msg) endNATS.Sub("test_subject", nothing, 1)
julia> publish(nc, "test_subject", "Hello.")
julia> sleep(0.2) # Wait for message.payload(msg) = "Hello."
julia> drain(nc, sub)
julia> publish(nc, "test_subject", "Hello.") # Ater drain msg won't be delivared.

Request reply

> nats reply help.please 'OK, I CAN HELP!!!'

20:35:19 Listening on "help.please" in group "NATS-RPLY-22"
julia> using NATS
julia> nc = NATS.connect()NATS.Connection(unnamed cluster, CONNECTED, 0 subs, 0 unsubs)
julia> rep = @time NATS.request(nc, "help.please"); 0.074377 seconds (56.99 k allocations: 3.985 MiB, 98.82% compilation time)
julia> payload(rep)"OK, I CAN HELP!!!"

Work queues

If subscription or reply is configured with queue_group, messages will be distributed equally between subscriptions with the same group.

julia> using NATS
julia> connection = NATS.connect()NATS.Connection(unnamed cluster, CONNECTED, 0 subs, 0 unsubs)
julia> sub1 = reply(connection, "some_subject"; queue_group="group1") do "Reply from worker 1" endNATS.Sub("some_subject", "group1", 1)
julia> sub2 = reply(connection, "some_subject"; queue_group="group1") do "Reply from worker 2" endNATS.Sub("some_subject", "group1", 2)
julia> @time request(String, connection, "some_subject") 0.018539 seconds (6.95 k allocations: 559.086 KiB, 96.04% compilation time) "Reply from worker 2"
julia> @time request(String, connection, "some_subject") 0.000537 seconds (131 allocations: 137.516 KiB) "Reply from worker 2"
julia> @time request(String, connection, "some_subject") 0.000553 seconds (131 allocations: 137.516 KiB) "Reply from worker 2"
julia> @time request(String, connection, "some_subject") 0.000571 seconds (131 allocations: 137.516 KiB) "Reply from worker 2"
julia> @time request(String, connection, "some_subject") 0.000617 seconds (131 allocations: 137.516 KiB) "Reply from worker 2"
julia> @time request(String, connection, "some_subject") 0.000586 seconds (134 allocations: 137.641 KiB) "Reply from worker 1"
julia> @time request(String, connection, "some_subject") 0.000548 seconds (131 allocations: 137.516 KiB) "Reply from worker 2"
julia> @time drain(connection, sub1) 0.201347 seconds (23 allocations: 984 bytes)
julia> @time drain(connection, sub2) 0.201380 seconds (23 allocations: 984 bytes)