Natsex v0.7.0 Natsex View Source

Elixir client for NATS.

Examples

iex(1)>{:ok, pid} = Natsex.start_link
{:ok, #PID<0.178.0>}

iex(2)> Natsex.subscribe(pid, "telegram.user.notifications", self())
"13b2d0cd-9dba-43b6-bb5d-288d48346ff4"

iex(3)> flush
{:natsex_message,
 {"telegram.user.notifications", "13b2d0cd-9dba-43b6-bb5d-288d48346ff4", nil},
 "Good news, everyone!"}
:ok

# sent a message and waits a response, aka "Request-Reply"
iex(4)> Natsex.request(pid, "questions", "sup?")
{:ok, "response"}

Link to this section Summary

Functions

Sending a message and waiting a response, aka “Request-Reply”

Starts Natsex client process

Stop client

Initiates a subscription to a subject. When new message will arrive, caller process will receive message

Unsubcribes the connection from the specified subject, or auto-unsubscribes after the specified number of messages has been received

Link to this section Functions

Link to this function publish(pid, subject, payload \\ "", reply \\ nil, timeout \\ 5000) View Source

Publishes the message to NATS

Examples

Natsex.publish(pid, "news.urgent", "today is monday")
:ok
Link to this function request(pid, subject, payload, timeout \\ 1000) View Source

Sending a message and waiting a response, aka “Request-Reply”

Examples

# wait response with default timeout 1sec
{:ok, response} = Natsex.request(pid, "questions", "sup?")

# wait response with custom timeout 10sec
:timeout = Natsex.request(pid, "questions", "sup?", 10_000)

Starts Natsex client process

Options

  • :config - Map that contains connection options (auth, host, port, etc)
  • :connect_timeout - Timeout for NATS server connection (default: 200 ms)
  • :ping_interval - interval for ping/pong keep-alive mechanism (default: 60_000 ms)
  • :reconnect_time_wait - timeout for reconnect (default: 1_000 ms)

Examples

# connects with default config, host - "localhost", port - 4222
Natsex.start_link
{:ok, #PID<0.194.0>}

# connects on custom port with credentials
Natsex.start_link(config: %{host: "localhost", port: 4567, user: "admin", pass: "12345"})
{:ok, #PID<0.195.0>}

# connects with timeout 2 sec
Natsex.start_link(connect_timeout: 2_000)
{:ok, #PID<>}
Link to this function subscribe(pid, subject, who, sid \\ nil, queue_group \\ nil) View Source

Initiates a subscription to a subject. When new message will arrive, caller process will receive message:

{:natsex_message, {subject, sid, nil}, message}

Examples

sid = Natsex.subscribe(pid, "news.urgent", self())
flush
{:natsex_message,
 {"telegram.user.notifications", "sub_id", nil},
 "Good news, everyone!"}
Link to this function unsubscribe(pid, sid, max_messages \\ nil) View Source

Unsubcribes the connection from the specified subject, or auto-unsubscribes after the specified number of messages has been received.

Examples

Natsex.unsubscribe(pid, "13b2d0cd-9dba-43b6-bb5d-288d48346ff4")
:ok