NSQ.Producer (elixir_nsq v1.2.0)

A producer is a process that connects to one or many NSQDs and publishes messages.

Interface

To initialize:

{:ok, producer} = NSQ.Producer.Supervisor.start_link("the-default-topic", %NSQ.Config{
  nsqds: ["127.0.0.1:6750"]
})

The default topic argument is required, even if you plan on explicitly publishing to a different topic. If you don't plan on using, you can set it to something like _default_topic_.

If you provide more than one nsqd, each pub/mpub will choose one randomly.

Note that, unlike consumers, producers cannot be configured to use discovery with nsqlookupd. This is because discovery requires a topic and channel, and an nsqd will only appear in nsqlookupd if it has already published messages on that topic. So there's a chicken-and-egg problem. The recommended solution is to run NSQD on the same box where you're publishing, so your address is always 127.0.0.1 with a static port.

pub

Publish a single message to NSQD.

NSQ.Producer.pub(producer, "a message")
NSQ.Producer.pub(producer, "different-topic", "a message")

mpub

Publish a bunch of messages to NSQD atomically.

NSQ.Producer.mpub(producer, ["one", "two"])
NSQ.Producer.mpub(producer, "different-topic", ["one", "two"])

Summary

Types

A tuple with a string ID (used to target the connection in NSQ.Connection.Supervisor) and a PID of the connection.

A tuple with a host and a port.

A map, but we can be more specific by asserting some entries that should be set for a connection's state map.

Functions

Returns a specification to start this module under a supervisor.

Create supervised connections to NSQD.

The end-user will be targeting the supervisor, but it's the producer that can actually handle the command.

Get the current state of a producer. Used in tests. Not for external use.

Callback implementation for GenServer.init/1.

Publish data to whatever topic is the default.

Publish data to a specific topic.

Publish data to whatever topic is the default.

Publish data to a specific topic.

Types

Link to this type

connection()

@type connection() :: {String.t(), pid()}

A tuple with a string ID (used to target the connection in NSQ.Connection.Supervisor) and a PID of the connection.

Link to this type

host_with_port()

@type host_with_port() :: {String.t(), integer()}

A tuple with a host and a port.

@type pro_state() :: %{conn_sup_pid: pid(), config: NSQ.Config.t()}

A map, but we can be more specific by asserting some entries that should be set for a connection's state map.

Functions

Link to this function

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function

connect_to_nsqds(nsqds, pro, pro_state)

@spec connect_to_nsqds([host_with_port()], pid(), pro_state()) :: {:ok, pro_state()}

Create supervised connections to NSQD.

@spec get(pid()) :: pid()

The end-user will be targeting the supervisor, but it's the producer that can actually handle the command.

Link to this function

get_connections(pro_state)

@spec get_connections(pro_state()) :: [connection()]
Link to this function

get_connections(pro, pro_state \\ nil)

@spec get_connections(pid(), pro_state()) :: [connection()]
Link to this function

get_state(producer)

@spec get_state(pid()) :: pro_state()

Get the current state of a producer. Used in tests. Not for external use.

Link to this function

init(pro_state)

@spec init(pro_state()) :: {:ok, pro_state()}

Callback implementation for GenServer.init/1.

Link to this function

mpub(sup_pid, data)

@spec mpub(pid(), binary()) :: {:ok, binary()}

Publish data to whatever topic is the default.

Link to this function

mpub(sup_pid, topic, data)

@spec mpub(pid(), binary(), binary()) :: {:ok, binary()}

Publish data to a specific topic.

Link to this function

pub(sup_pid, data)

@spec pub(pid(), binary()) :: {:ok, binary()}

Publish data to whatever topic is the default.

Link to this function

pub(sup_pid, topic, data)

@spec pub(pid(), binary(), binary()) :: {:ok, binary()}

Publish data to a specific topic.

Link to this function

random_connection_pid(pro_state)

@spec random_connection_pid(pro_state()) :: pid()
Link to this function

start_link(arg)

@spec start_link({binary(), NSQ.Config.t()}) :: {:ok, pid()}
@spec start_link({binary(), NSQ.Config.t(), GenServer.options()}) :: {:ok, pid()}