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 — Function
publish(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 published
  • deadline: absolute enqueue deadline as a Nanosecond value on the time_ns() clock; defaults to no deadline. Expiry throws NATS.SendDeadlineExceeded. Messages already queued are not cancelled.

Examples:

    publish(nc, "some_subject", "Some payload")
    publish("some_subject", ("Some payload", ["some_header" => "Example header value"]))
source
NATS.subscribe — Function
subscribe(
    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 members
  • spawn: if true a task will be spawned for each f invocation, otherwise messages are processed sequentially, default is false
  • channel_size: maximum number of items buffered for processing, if full the messages will be ignored, default is 524288, can be configured globally with NATS_SUBSCRIPTION_CHANNEL_SIZE env variable
source
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 members
  • channel_size: maximum number of items buffered for processing, if full the messages will be ignored, default is 524288, can be configured globally with NATS_SUBSCRIPTION_CHANNEL_SIZE env variable
source
NATS.next — Function
next(connection, sub; no_wait, no_throw)

Obtains the next message for synchronous subscription.

Optional keyword arguments:

  • no_wait: do not wait for next message, return nothing if buffer is empty
  • no_throw: do not throw exception, returns nothing if cannot get next message
source
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, return nothing if buffer is empty
  • no_throw: do not throw exception, returns nothing if cannot get next message
source
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 empty
  • no_throw: do not throw exception, stops collecting messages on error
source
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 empty
  • no_throw: do not throw exception, stops collecting messages on error
source
NATS.unsubscribe — Function
unsubscribe(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 the unsubscribe message is received on the server side, what can occur after some time lag
source
NATS.drain — Function
drain(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.

source
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 until timeout expires. Default 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.
source