The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

POEx::ZMQ3::Subscriber

SYNOPSIS

  use POE;

  my $zsub = POEx::ZMQ3::Subscriber->new();
  
  POE::Session->create(
    inline_states => {

      _start => sub {
        ## Connect to a ZeroMQ publisher:
        $zsub->start( 'tcp://127.0.0.1:5665' );

        ## Our session wants all emitted events:
        $_[KERNEL]->post( $zsub->session_id,
          'subscribe',
          'all'
        );
      },

      zeromq_subscribed_to => sub {
        my $target = $_[ARG0];
        print "Subscribed to $target\n";
      },

      zeromq_received => sub {
        my $data = $_[ARG0];
        print "Received $data from publisher\n";

        if (++$_[HEAP]->{count} == 1000) {
          warn "I don't want any more messages :(";
          $zsub->stop;
        }
      },

    },
  );

  $poe_kernel->run;

DESCRIPTION

A lightweight ZeroMQ subscriber-type socket using POEx::ZMQ3::Role::Emitter.

This is a simple subscriber; by default it indiscriminately receives all published messages without filtering.

Methods

start

  $zsub->start( $subscribe_to );

Start the Subscriber and connect to a specified target.

stop

  $zsub->stop;

Stop the Subscriber, closing out the socket and stopping the event emitter.

Events

zeromq_subscribed_to

Emitted when we are initialized; $_[ARG0] is the target publisher's address.

zeromq_received

Emitted when we receive data from the publisher we are subscribed to

If this is a single-part message, $_[ARG0] is the (raw) data received.

If this is a multi-part message, slurp the argument array to receive all parts:

  sub zeromq_received {
    my @parts    = @_[ARG0 .. $#_];
    my $envelope = shift @parts;
    . . .
  }

Attributes

targets

An ARRAY containing the list of publishing endpoints the Subscriber was configured for; see "start".

SEE ALSO

POEx::ZMQ3

POEx::ZMQ3::Publisher

ZMQ::LibZMQ3

http://www.zeromq.org

AUTHOR

Jon Portnoy <avenj@cobaltirc.org>