Why not adopt me?
NAME
POEx::ZMQ3::Sockets - POE-enabled ZeroMQ sockets
SYNOPSIS
## A 'REQ' client that sends 'PING' to a REP on localhost:5050
use strictures 1;
use POE;
use POEx::ZMQ3::Sockets;
POE::Session->create(
package_states => [
main => [ qw/
_start
zmqsock_registered
zmqsock_recv
/ ],
]
);
sub _start {
my ($kern, $heap) = @_[KERNEL, HEAP];
my $zmq = POEx::ZMQ3::Sockets->new;
$zmq->start;
$heap->{zmq} = $zmq;
$kern->call( $zmq->session_id, 'subscribe', 'all' );
}
sub zmqsock_registered {
my ($kern, $heap) = @_[KERNEL, HEAP];
my $zmq = $heap->{zmq};
$zmq->create( 'pinger', 'REQ' );
$zmq->connect( 'pinger', 'tcp://127.0.0.1:5050' );
$zmq->write( 'pinger', 'PING' );
}
sub zmqsock_recv {
my ($kern, $heap) = @_[KERNEL, HEAP];
my ($alias, $zmsg, $data) = @_[ARG0 .. $#_];
if ($data eq 'PONG') {
## Got a PONG. Send another PING:
$zmq->write( 'pinger', 'PING' );
}
}
$poe_kernel->run;
DESCRIPTION
This is the backend MooX::Role::POE::Emitter session behind POEx::ZMQ3, integrating ZeroMQ (http://www.zeromq.org) with a POE event loop.
Registering Sessions
Your POE::Session should register with the component to receive events:
## Inside a POE::Session
## Get all events from component in $_[HEAP]->{zmq}:
sub my_start {
my $zmq = $_[HEAP]->{zmq};
$_[KERNEL]->call( $zmq->session_id, 'subscribe', 'all' );
}
See "POE API" for more on events emitted and accepted by this component.
See MooX::Role::POE::Emitter for more details on event emitters.
Methods
start
Takes no arguments.
Spawns the MooX::Role::POE::Emitter session that controls ZMQ socket handling. Must be called prior to operating on sockets.
stop
Takes no arguments.
Stops the component, closing out all active sockets.
create
Takes a socket alias and a socket type.
Creates a new ZeroMQ socket. The socket is not initially bound/connected to anything; see "bind", "connect".
The socket type may be either a constant from ZMQ::Constants or a string type:
## Equivalent:
$zmq->create( $alias, 'PUB' );
use ZMQ::Constants 'ZMQ_PUB';
$zmq->create( $alias, ZMQ_PUB );
See the zmq_socket man page for details.
bind
Takes a socket alias and an endpoint to listen for connections on.
The opposite of "bind" is "connect"
connect
Takes a socket alias and a target endpoint to connect to.
Note that ZeroMQ manages its own connections asynchronously. A successful "bind" or "connect" is not necessarily indicative of a positively usable connection.
write
Takes a socket alias, some data (as a scalar), and optional flags to pass to ZeroMQ's zmq_msg_send:
## Write a simple message:
$zmq->write( $alias, 'A message' );
## Write some serialized data:
my $ref = { things => 'some data' };
my $data = Storable::nfreeze( $ref );
$zmq->write( $alias, $data );
Writes data to the ZMQ socket, when possible.
close
Takes a socket alias.
Closes the specified ZMQ socket.
context
Takes no arguments.
Returns the current POEx::ZMQ3::Context object.
get_zmq_socket
Takes a socket alias.
Returns the actual ZMQ::LibZMQ3 socket object.
set_zmq_sockopt
Takes a socket alias and arbitrary flags/options to pass to zmq_setsockopt.
See the man page for zmq_setsockopt.
set_zmq_subscribe
Takes a socket alias and an optional subscription prefix.
Calls "set_zmq_sockopt" to set the ZMQ_SUBSCRIBE
flag for the specified socket; this is used by SUB-type sockets to subscribe to messages.
If no subscription prefix is specified, the socket will be subscribed to all messages.
POE API
Emitted Events
zmqsock_bind_added
Emitted when a "bind" has been executed.
$_[ARG0] is the socket's alias.
$_[ARG1] is the endpoint string.
zmqsock_connect_added
Emitted when a "connect" has been executed.
$_[ARG0] is the socket's alias.
$_[ARG1] is the endpoint string.
zmqsock_recv
Emitted when some data has been received on a socket.
$_[ARG0] is the socket's alias.
$_[ARG1] is the actual ZMQ::LibZMQ3 message object.
$_[ARG2] is the raw message data extracted via zmq_msg_data.
zmqsock_created
Emitted when a socket has been created.
$_[ARG0] is the alias that was spawned.
$_[ARG1] is the socket's type, as a ZMQ::Constants constant.
zmqsock_closing
Emitted when a socket is being shut down.
$_[ARG0] is the alias that is closing.
Accepted Events
The following events take the same parameters as their counterparts described in "Methods":
create
bind
connect
write
close
SEE ALSO
AUTHOR
Jon Portnoy <avenj@cobaltirc.org>