Request - Reply

Functions implementing the Request-Reply pattern.

NATS.request — Function
request(connection, subject; ...)
request(connection, subject, data; timeout)

Send a NATS Request-Reply message.

The default timeout is 5.0 seconds which can be overridden by passing timeout. It can be configured globally with the NATS_REQUEST_TIMEOUT_SECONDS env variable.

Optional keyword arguments are:

  • timeout: error will be thrown if no replies are received until timeout number of seconds, NATS.SendDeadlineExceeded if the request could not be queued for sending in that time

Examples

julia> NATS.request(connection, "help.please")
MSG _INBOX.VnRLq6t91z.1 0 17
OK, I CAN HELP!!!

julia> request(connection, "help.please"; timeout = 0)
ERROR: Send deadline exceeded.
source
request(connection, nreplies, subject; ...)
request(
    connection,
    nreplies,
    subject,
    data;
    timeout,
    stop_on_status
)

Requests for multiple replies. Vector of messages is returned after receiving nreplies replies, a reply with one of stop_on_status codes or the timer expired. Can return less messages than specified (also an empty array if a timeout occurs), errors are not filtered.

Optional keyword arguments are:

  • timeout: an empty vector will be returned if no replies are received until timeout number of seconds
  • stop_on_status: a collection of status codes, a reply with one of them is the last collected and the remaining replies are not waited for. By default it is every error status (400:599), what includes 503 sent by the server when there are no responders. Replies with other statuses are collected as any other reply, pass an empty collection to always wait for nreplies replies or the timeout.

Examples

julia> request(connection, 2, "help.please"; timeout = 0)
ERROR: Send deadline exceeded.
source

Request a reply from a service listening for subject messages. Reply is converted to specified type. An appropriate convert method must be defined, otherwise an error is thrown.

source
NATS.reply — Function
reply(f, connection, subject; queue_group, spawn)

Reply for messages for a subject. Works like subscribe with automatic publish to the subject from reply_to field.

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

Examples

julia> sub = reply("FOO.REQUESTS") do msg
    "This is a reply payload."
end
NATS.Sub("FOO.REQUESTS", nothing, "jdnMEcJN")

julia> sub = reply("FOO.REQUESTS") do msg
    "This is a reply payload.", ["example_header" => "This is a header value"]
end
NATS.Sub("FOO.REQUESTS", nothing, "jdnMEcJN")

julia> unsubscribe(sub)
source