Module NATS
In: lib/nats/client.rb
lib/nats/version.rb
lib/nats/nuid.rb

Copyright 2010-2018 The NATS Authors Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Methods

Classes and Modules

Class NATS::AuthError
Class NATS::ClientError
Class NATS::ConnectError
Class NATS::Error
Class NATS::MonotonicTime
Class NATS::NUID
Class NATS::ServerError

Constants

DEFAULT_PORT = 4222
DEFAULT_URI = "nats://localhost:#{DEFAULT_PORT}".freeze
MAX_RECONNECT_ATTEMPTS = 10
RECONNECT_TIME_WAIT = 2
MAX_PENDING_SIZE = 32768
FAST_PRODUCER_THRESHOLD = (10*1024*1024)   Maximum outbound size per client to trigger FP, 20MB
DEFAULT_PING_INTERVAL = 120   Ping intervals
DEFAULT_PING_MAX = 2
DEFAULT_DRAIN_TIMEOUT = 30   Drain mode support
VERSION = "0.10.0".freeze   NOTE: These are all announced to the server on CONNECT
LANG = RUBY_ENGINE
PROTOCOL_VERSION = 1

External Aliases

reactor_was_running -> reactor_was_running?
connected -> connected?
closing -> closing?
reconnecting -> reconnecting?
draining -> draining?

Attributes

bytes_received  [R] 
bytes_sent  [R] 
close_cb  [R] 
close_cb  [R] 
closing  [R] 
disconnect_cb  [R] 
disconnect_cb  [R] 
draining  [R] 
msgs_received  [R] 
msgs_sent  [R] 
options  [R] 
pings  [R] 
reconnect_cb  [R] 
reconnecting  [R] 
server_info  [R] 
server_pool  [R] 

Public Class methods

Create and return a connection to the server with the given options. The optional block will be called when the connection has been completed.

@param [String] uri The URI or comma separated list of URIs of NATS servers to connect to. @param [Hash] opts @option opts [String|URI] :uri The URI to connect to, example nats://localhost:4222 @option opts [Boolean] :reconnect Boolean that can be used to suppress reconnect functionality. @option opts [Boolean] :debug Boolean that can be used to output additional debug information. @option opts [Boolean] :verbose Boolean that is sent to server for setting verbose protocol mode. @option opts [Boolean] :pedantic Boolean that is sent to server for setting pedantic mode. @option opts [Boolean] :ssl Boolean that is sent to server for setting TLS/SSL mode. @option opts [Hash] :tls Map of options for configuring secure connection handled to EM#start_tls directly. @option opts [Integer] :max_reconnect_attempts Integer that can be used to set the max number of reconnect tries @option opts [Integer] :reconnect_time_wait Integer that can be used to set the number of seconds to wait between reconnect tries @option opts [Integer] :ping_interval Integer that can be used to set the ping interval in seconds. @option opts [Integer] :max_outstanding_pings Integer that can be used to set the max number of outstanding pings before declaring a connection closed. @param [Block] &blk called when the connection is completed. Connection will be passed to the block. @return [NATS] connection to the server.

@example Connect to local NATS server.

 NATS.connect do |nc|
   # ...
 end

@example Setting custom server URI to connect.

 NATS.connect("nats://localhost:4222") do |nc|
   # ...
 end

@example Setting username and password to authenticate.

 NATS.connect("nats://user:password@localhost:4222") do |nc|
   # ...
 end

@example Specifying explicit list of servers via options.

 NATS.connect(servers: ["nats://127.0.0.1:4222","nats://127.0.0.1:4223","nats://127.0.0.1:4224"]) do |nc|
   # ...
 end

@example Using comma separated array to define list of servers.

 NATS.connect("nats://localhost:4223,nats://localhost:4224") do |nc|
   # ...
 end

@example Only specifying endpoint uses NATS default scheme and port.

 NATS.connect("demo.nats.io") do |nc|
   # ...
 end

@example Setting infinite reconnect retries with 2 seconds back off against custom URI.

 NATS.connect("demo.nats.io:4222", max_reconnect_attempts: -1, reconnect_time_wait: 2) do |nc|
   # ...
 end

@return [Boolean] Connected state

@return [URI] Connected server

Returns a subject that can be used for "directed" communications. @return [String]

Drain gracefully disconnects from the server, letting subscribers process pending messages already sent by server and optionally calls the associated block. @param [Block] &blk called when drain is done and connection is closed.

@return [Boolean] Draining state

Flushes all messages and subscriptions in the default connection @see NATS#flush

Set the default on_closed callback. @param [Block] &callback called when will reach a state when will no longer be connected.

Set the default on_disconnect callback. @param [Block] &callback called whenever client disconnects from a server.

Set the default on_error callback. @param [Block] &callback called when an error has been detected.

Set the default on_reconnect callback. @param [Block] &callback called when a reconnect attempt is made.

@return [Hash] Options

Return bytes outstanding for the default client connection. @see NATS#pending_data_size

Publish a message using the default client connection. @see NATS#publish

@return [Boolean] Reconnecting state

Publish a message and wait for a response on the default client connection. @see NATS#request

@return [Hash] Server information

Create a default client connection to the server. @see NATS::connect

Close the default client connection and optionally call the associated block. @param [Block] &blk called when the connection is closed.

Subscribe using the default client connection. @see NATS#subscribe

Set a timeout for receiving messages for the subscription. @see NATS#timeout

Cancel a subscription on the default client connection. @see NATS#unsubscribe

Public Instance methods

Close the connection to the server.

@return [URI] Connected server

Retrieves the list of servers which have been discovered via server connect_urls announcements

Drain gracefully closes the connection. @param [Block] blk called when drain is done and connection is closed.

Flushes all messages and subscriptions for the connection. All messages and subscriptions have been processed by the server when the optional callback is called.

Define a callback to be called when client is disconnected from server. @param [Block] &callback called when will reach a state when will no longer be connected.

Define a callback to be called when the client connection has been established. @param [Block] callback

Define a callback to be called when client is disconnected from server. @param [Block] &callback called whenever client disconnects from a server.

Define a callback to be called when errors occur on the client connection. @param [Block] &callback called when an error has been detected.

Define a callback to be called when a reconnect attempt is made. @param [Block] &callback called when a reconnect attempt is made.

Return bytes outstanding waiting to be sent to server.

Parse out URIs which can now be an array of server choices The server pool will contain both explicit and implicit members.

Publish a message to a given subject, with optional reply subject and completion block @param [String] subject @param [Object, to_s] msg @param [String] opt_reply @param [Block] blk, closure called when publish has been processed by the server.

Send a request and have the response delivered to the supplied callback. @param [String] subject @param [Object] msg @param [Block] callback @return [Object] sid

Subscribe to a subject with optional wildcards. Messages will be delivered to the supplied callback. Callback can take any number of the supplied arguments as defined by the list: msg, reply, sub. Returns subscription id which can be passed to unsubscribe. @param [String] subject, optionally with wilcards. @param [Hash] opts, optional options hash, e.g. :queue, :max. @param [Block] callback, called when a message is delivered. @return [Object] sid, Subject Identifier

Return the active subscription count. @return [Number]

Setup a timeout for receiving messages for the subscription. @param [Object] sid @param [Number] timeout, float in seconds @param [Hash] opts, options, :auto_unsubscribe(true), :expected(1)

Cancel a subscription. @param [Object] sid @param [Number] opt_max, optional number of responses to receive before auto-unsubscribing

[Validate]