NatsEx.Connection (nats_ex v0.3.0)

A GenServer implementing a connection to Natsd server.

You can set options for Nats in your config.exs. For example

config :nats_ex, host: "localhost", port: 4222

Supported options are:

  • username: Username, if auth is required
  • password: Password, if auth is required
  • host: Host for Nats server. Defaults to localhost
  • port: Port for Nats server. Defaults to 4222

For example,

iex> {:ok, conn} = NatsEx.Connection.connection
{:ok, #PID<0.153.0>}
iex> NatsEx.Connection.sub("foo")
:ok
iex> NatsEx.Connection.pub("foo", "hey")
:ok
iex> flush()
{:nats_ex, :msg, "foo", nil, "hey"} # See `NatsEx.Connection.sub/2` for more details about message format

Link to this section Summary

Functions

Returns a specification to start this module under a supervisor.

Opens a connection

For subscribing to any subject

For unsubscribing from a certain subject.

Link to this section Functions

Link to this function

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

Specs

Opens a connection

Link to this function

pub(conn, subject, payload, reply_to \\ nil)

Specs

pub(pid(), String.t(), String.t(), String.t() | nil) :: :ok

For publishing.

reply_to is optional. Returns :ok

Link to this function

require_auth?(info)

Specs

require_auth?(map()) :: boolean()
Link to this function

send_subcriber_message(list, sid, subject, rep_to, payload)

Sends messages to subscribers.

Checks if the process is supposed to unsubscribe after the message is received.

Link to this function

send_subscriber_message(list, sid, subject, rep_to, payload)

Link to this function

sub(conn, subject, queue_group \\ nil)

Specs

sub(pid(), String.t(), integer()) :: :ok

For subscribing to any subject

queue_group is optional

When a new message arrives, the subscribed process gets a message. The format of the message is {:nats_ex, subject, reply_to_subject, payload}

Link to this function

unsub(conn, subject, num_of_msgs \\ nil)

Specs

unsub(pid(), String.t(), integer() | nil) :: :ok

For unsubscribing from a certain subject.

num_of_msgs is the max number of messages received, after which it automatically unsubscribes. It is optional