ex_nsq v1.1.0 NSQ.Producer
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"])
Link to this section 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
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
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
Link to this section 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.
Link to this section Functions
connect_to_nsqds([host_with_port], pid, pro_state) :: {:ok, pro_state}
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_connections(pid, pro_state) :: [connection]
Get the current state of a producer. Used in tests. Not for external use.
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.