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 13.686180114746094 milliseconds 0.016472 seconds (9.45 k allocations: 720.477 KiB, 97.23% compilation time) NATS.Pong()
julia> @time NATS.ping(nc)
[ Info: Measured ping time is 0.2918243408203125 milliseconds 0.002514 seconds (113 allocations: 136.688 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) end
NATS.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.066700 seconds (55.81 k allocations: 3.903 MiB, 98.53% 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" end
NATS.Sub("some_subject", "group1", 1)
julia> sub2 = reply(connection, "some_subject"; queue_group="group1") do "Reply from worker 2" end
NATS.Sub("some_subject", "group1", 2)
julia> @time request(String, connection, "some_subject")
0.017881 seconds (6.90 k allocations: 554.148 KiB, 94.85% compilation time) "Reply from worker 2"
julia> @time request(String, connection, "some_subject")
0.000684 seconds (158 allocations: 138.875 KiB) "Reply from worker 2"
julia> @time request(String, connection, "some_subject")
0.000702 seconds (158 allocations: 138.875 KiB) "Reply from worker 2"
julia> @time request(String, connection, "some_subject")
0.000695 seconds (159 allocations: 138.953 KiB) "Reply from worker 1"
julia> @time request(String, connection, "some_subject")
0.000717 seconds (158 allocations: 138.875 KiB) "Reply from worker 2"
julia> @time request(String, connection, "some_subject")
0.000640 seconds (159 allocations: 139.938 KiB) "Reply from worker 2"
julia> @time request(String, connection, "some_subject")
0.000623 seconds (158 allocations: 138.875 KiB) "Reply from worker 2"
julia> @time drain(connection, sub1)
0.201315 seconds (25 allocations: 1016 bytes)
julia> @time drain(connection, sub2)
0.201367 seconds (25 allocations: 1016 bytes)