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 compilation[ Info: Measured ping time is 0.4260540008544922 milliseconds 0.005273 seconds (500 allocations: 100.516 KiB, 47.54% compilation time) NATS.Pong()julia> @time NATS.ping(nc)[ Info: Measured ping time is 0.4718303680419922 milliseconds 0.001675 seconds (100 allocations: 72.242 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.") # After drain msg won't be delivered.
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.000753 seconds (55 allocations: 5.195 KiB)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.014972 seconds (5.94 k allocations: 407.883 KiB, 94.45% compilation time) "Reply from worker 2"julia> @time request(String, connection, "some_subject")0.000510 seconds (64 allocations: 6.289 KiB) "Reply from worker 1"julia> @time request(String, connection, "some_subject")0.000499 seconds (63 allocations: 6.211 KiB) "Reply from worker 1"julia> @time request(String, connection, "some_subject")0.000405 seconds (63 allocations: 6.211 KiB) "Reply from worker 2"julia> @time request(String, connection, "some_subject")0.000524 seconds (63 allocations: 6.211 KiB) "Reply from worker 2"julia> @time request(String, connection, "some_subject")0.000461 seconds (63 allocations: 6.211 KiB) "Reply from worker 2"julia> @time request(String, connection, "some_subject")0.000769 seconds (63 allocations: 6.211 KiB) "Reply from worker 1"julia> @time drain(connection, sub1)0.201441 seconds (15 allocations: 632 bytes)julia> @time drain(connection, sub2)0.201382 seconds (15 allocations: 632 bytes)