Debugging
Monitoring connection state
julia> using NATS
julia> nc = NATS.connect()
NATS.Connection(unnamed cluster, CONNECTED, 0 subs, 0 unsubs)
julia> @async NATS.status_change(nc) do state @info "Connection to $(NATS.clustername(nc)) changed state to $state" end
Task (runnable) @0x00007f93f1114c90
julia> NATS.reconnect(nc)
┌ Warning: Connection to unnamed cluster on `nats://localhost:4222` lost, trynig to reconnect. └ @ NATS ~/work/NATS.jl/NATS.jl/src/connection/connect.jl:385 [ Info: Connection to unnamed changed state to CONNECTING
julia> NATS.drain(nc)
[ Info: Reconnected to unnamed cluster on `nats://localhost:4222` after 0.018524169921875 seconds. [ Info: Connection to unnamed changed state to CONNECTED [ Info: Connection to unnamed changed state to DRAINING
julia> sleep(7) # Wait a moment to get DRAINED status as well
Connection and subscription statistics
There are detailed statistics of published and received messages collected. They can be accessed for each subscription and connection. Connection statistics aggregates stats for all its subscriptions.
julia> using NATS
julia> nc = NATS.connect()
NATS.Connection(unnamed cluster, CONNECTED, 0 subs, 0 unsubs)
julia> sub1 = subscribe(nc, "topic") do msg t = Threads.@spawn publish(nc, "other_topic", payload(msg)) wait(t) end
NATS.Sub("topic", nothing, 1)
julia> sub2 = subscribe(nc, "other_topic") do msg @show payload(msg) end
NATS.Sub("other_topic", nothing, 2)
julia> NATS.stats(nc)
published: 0 received: 0 pending: 0 active: 0 handled: 0 errored: 0 dropped: 0
julia> publish(nc, "topic", "Hi!")
julia> NATS.stats(nc)
published: 1 received: 1 pending: 1 active: 0 handled: 0 errored: 0 dropped: 0
julia> NATS.stats(nc, sub1)
published: 1 received: 1 pending: 0 active: 0 handled: 1 errored: 0 dropped: 0
julia> NATS.stats(nc, sub2)
published: 0 received: 1 pending: 1 active: 0 handled: 0 errored: 0 dropped: 0
julia> sleep(0.1) # Wait for message to be propagated
payload(msg) = "Hi!"
julia> NATS.stats(nc)
published: 2 received: 2 pending: 0 active: 0 handled: 2 errored: 0 dropped: 0
julia> NATS.stats(nc, sub1)
published: 1 received: 1 pending: 0 active: 0 handled: 1 errored: 0 dropped: 0
julia> NATS.stats(nc, sub2)
published: 0 received: 1 pending: 0 active: 0 handled: 1 errored: 0 dropped: 0
julia> NATS.drain(nc)