Publish - Subscribe
Functions implementing the NATS Publish-Subscribe distribution model. For queue group 1:1 instead of the default 1:N fanout configure subscriptions with the same queue_group argument.
NATS.publish — Functionpublish(connection, subject; ...)
publish(connection, subject, data; reply_to, deadline)
Publish data to a subject, the payload is obtained with the show method taking mime application/nats-payload, headers are obtained with the show method taking mime application/nats-headers.
There are predefined conversions defined for the String type. To publish headers there is a defined conversion from a tuple taking a vector of pairs of strings.
Optional parameters:
reply_to: subject to which a result should be publisheddeadline: absolute enqueue deadline as aNanosecondvalue on thetime_ns()clock; defaults to no deadline. Expiry throwsNATS.SendDeadlineExceeded. Messages already queued are not cancelled.
Examples:
publish(nc, "some_subject", "Some payload")
publish("some_subject", ("Some payload", ["some_header" => "Example header value"]))NATS.subscribe — Functionsubscribe(
f,
connection,
subject;
queue_group,
spawn,
channel_size
)
Subscribe to a subject.
Optional keyword arguments are:
queue_group: NATS server will distribute messages across queue group membersspawn: iftruea task will be spawned for eachfinvocation, otherwise messages are processed sequentially, default isfalsechannel_size: maximum number of items buffered for processing, if full the messages will be ignored, default is524288, can be configured globally withNATS_SUBSCRIPTION_CHANNEL_SIZEenv variable
subscribe(connection, subject; queue_group, channel_size)
Subscribe to a subject in synchronous mode. The client is supposed to call next manually to obtain messages.
Optional keyword arguments are:
queue_group: NATS server will distribute messages across queue group memberschannel_size: maximum number of items buffered for processing, if full the messages will be ignored, default is524288, can be configured globally withNATS_SUBSCRIPTION_CHANNEL_SIZEenv variable
NATS.next — Functionnext(connection, sub; no_wait, no_throw)
Obtains the next message for synchronous subscription.
Optional keyword arguments:
no_wait: do not wait for next message, returnnothingif buffer is emptyno_throw: do not throw exception, returnsnothingif cannot get next message
next(T, connection, sub; no_wait, no_throw)
Obtains the next message for synchronous subscription converting it to requested T type.
Optional keyword arguments:
no_wait: do not wait for next message, returnnothingif buffer is emptyno_throw: do not throw exception, returnsnothingif cannot get next message
next(connection, sub, batch; no_wait, no_throw)
Obtains batch of messages for synchronous subscription.
Optional keyword arguments:
no_wait: do not wait for next message, return an empty vector if buffer is emptyno_throw: do not throw exception, stops collecting messages on error
next(T, connection, sub, batch; no_wait, no_throw)
Obtains batch of messages for synchronous subscription converting them to the requested T type.
Optional keyword arguments:
no_wait: do not wait for next message, return an empty vector if buffer is emptyno_throw: do not throw exception, stops collecting messages on error
NATS.unsubscribe — Functionunsubscribe(connection, sub; max_msgs)
Unsubscribe from a subject. sub is an object returned from subscribe or reply.
Optional keyword arguments are:
max_msgs: maximum number of messages the server will send after theunsubscribemessage is received on the server side, what can occur after some time lag
NATS.drain — Functiondrain(connection)
Unsubscribe all subscriptions, wait for processing all messages in the buffers, then close the connection. A drained connection is no more usable. This method is used to gracefully stop the process.
Underneath it periodically checks for the state of all buffers, the interval for checks is configurable per connection with the drain_poll parameter of the connect method. It can also be set globally with the NATS_DRAIN_POLL_INTERVAL_SECONDS environment variable. If not set explicitly the default polling interval is 0.2 seconds.
An error will be written to the log if the drain is not finished until the timeout expires. The default timeout value is configurable per connection on connect with drain_timeout. It can be also set globally with the NATS_DRAIN_TIMEOUT_SECONDS environment variable. If not set explicitly the default drain timeout is 5.0 seconds.
If connection is reconnecting, drain may wait for connection attempt to finish. Time spent reconnecting is not included in drain_timeout.
drain(connection, sub; timeout)
Unsubscribe a subscription and wait for processing all messages in the buffer.
Underneath it periodically checks for the state of the buffer, the interval for checks is configurable per connection with the drain_poll parameter of the connect method. It can also be set globally with the NATS_DRAIN_POLL_INTERVAL_SECONDS environment variable. If not set explicitly the default polling interval is 0.2 seconds.
Optional keyword arguments:
timeout: an error will be thrown if the drain is not finished untiltimeoutexpires. Default value is configurable per connection onconnectwithdrain_timeout. It can be also set globally with theNATS_DRAIN_TIMEOUT_SECONDSenvironment variable. If not set explicitly the default drain timeout is5.0seconds.