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: 4222Supported options are:
username: Username, if auth is requiredpassword: Password, if auth is requiredhost: Host for Nats server. Defaults tolocalhostport: Port for Nats server. Defaults to4222
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 publishing.
Sends messages to subscribers.
For subscribing to any subject
For unsubscribing from a certain subject.
Link to this section Functions
child_spec(init_arg)
Returns a specification to start this module under a supervisor.
See Supervisor.
connection()
Specs
connection() :: DynamicSupervisor.on_start_child()
Opens a connection
pub(conn, subject, payload, reply_to \\ nil)
Specs
For publishing.
reply_to is optional. Returns :ok
require_auth?(info)
Specs
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.
send_subscriber_message(list, sid, subject, rep_to, payload)
sub(conn, subject, queue_group \\ nil)
Specs
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}
unsub(conn, subject, num_of_msgs \\ nil)
Specs
For unsubscribing from a certain subject.
num_of_msgs is the max number of messages received, after which it automatically unsubscribes.
It is optional