Quick start
Start nats-server:
docker run -p 4222:4222 nats:latestCheck ping - pong working
julia> using NATSjulia> nc = NATS.connect()NATS.Connection(unnamed cluster, CONNECTED, 0 subs, 0 unsubs)julia> @time NATS.ping(nc) # First one may be slow due to compilation0.119417 seconds (56.63 k allocations: 4.013 MiB, 6.32% gc time, 99.66% compilation time) NATS.Pong()julia> @time NATS.ping(nc)0.000323 seconds (37 allocations: 66.781 KiB) NATS.Pong()
Publish subscribe
julia> using NATSjulia> 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 NATSjulia> nc = NATS.connect()NATS.Connection(unnamed cluster, CONNECTED, 0 subs, 0 unsubs)julia> rep = @time NATS.request(nc, "help.please");0.070474 seconds (57.01 k allocations: 3.986 MiB, 98.78% 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 NATSjulia> 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.018661 seconds (6.97 k allocations: 560.617 KiB, 95.61% compilation time) "Reply from worker 2"julia> @time request(String, connection, "some_subject")0.000539 seconds (146 allocations: 139.047 KiB) "Reply from worker 2"julia> @time request(String, connection, "some_subject")0.000683 seconds (146 allocations: 139.047 KiB) "Reply from worker 2"julia> @time request(String, connection, "some_subject")0.000685 seconds (146 allocations: 139.047 KiB) "Reply from worker 2"julia> @time request(String, connection, "some_subject")0.000666 seconds (146 allocations: 139.047 KiB) "Reply from worker 2"julia> @time request(String, connection, "some_subject")0.000677 seconds (146 allocations: 139.047 KiB) "Reply from worker 2"julia> @time request(String, connection, "some_subject")0.000712 seconds (149 allocations: 139.172 KiB) "Reply from worker 1"julia> @time drain(connection, sub1)0.201323 seconds (23 allocations: 984 bytes)julia> @time drain(connection, sub2)0.201350 seconds (23 allocations: 984 bytes)