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[ Info: Measured ping time is 12.953042984008789 milliseconds 0.015701 seconds (9.44 k allocations: 719.648 KiB, 96.57% compilation time) NATS.Pong()
julia> @time NATS.ping(nc)[ Info: Measured ping time is 0.26607513427734375 milliseconds 0.002497 seconds (108 allocations: 136.578 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.061456 seconds (55.80 k allocations: 3.899 MiB, 98.27% 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.017820 seconds (6.89 k allocations: 553.992 KiB, 93.83% compilation time) "Reply from worker 1"
julia> @time request(String, connection, "some_subject") 0.000794 seconds (149 allocations: 138.797 KiB) "Reply from worker 2"
julia> @time request(String, connection, "some_subject") 0.000726 seconds (148 allocations: 138.719 KiB) "Reply from worker 1"
julia> @time request(String, connection, "some_subject") 0.000762 seconds (148 allocations: 138.719 KiB) "Reply from worker 2"
julia> @time request(String, connection, "some_subject") 0.000934 seconds (148 allocations: 138.719 KiB) "Reply from worker 2"
julia> @time request(String, connection, "some_subject") 0.000811 seconds (148 allocations: 138.719 KiB) "Reply from worker 2"
julia> @time request(String, connection, "some_subject") 0.000839 seconds (150 allocations: 140.344 KiB) "Reply from worker 2"
julia> @time drain(connection, sub1) 0.201378 seconds (25 allocations: 1016 bytes)
julia> @time drain(connection, sub2) 0.201408 seconds (25 allocations: 1016 bytes)