From a9d7134082f9185b8c41e254ac6c80f711c647d1 Mon Sep 17 00:00:00 2001 From: Pedro Melo Date: Sun, 20 Jul 2014 22:13:50 +0100 Subject: [PATCH] Add publish() to Write Signed-off-by: Pedro Melo --- lib/AnyEvent/NSQ/Writer.pm | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/AnyEvent/NSQ/Writer.pm b/lib/AnyEvent/NSQ/Writer.pm index 90294ed..6827cf9 100644 --- a/lib/AnyEvent/NSQ/Writer.pm +++ b/lib/AnyEvent/NSQ/Writer.pm @@ -10,5 +10,21 @@ use Carp 'croak'; use parent 'AnyEvent::NSQ::Client'; +#### Producer API + +## Publish a single message - callback is only called if we succedd +sub publish { + my ($self, $topic, $data, $cb) = @_; + + my $conn = $self->_random_connected_conn; + croak "ERROR: there no active connections at this moment," unless $conn; + + $cb = sub { $cb->($self, $topic, $data, @_) } + if $cb; + + return $conn->publish($topic, $data, $cb); +} + + 1;