POEx::ZMQ::Socket - A POE-enabled ZeroMQ socket
# An example ZMQ_ROUTER socket -> use POE; use POEx::ZMQ; POE::Session->create( inline_states => +{ _start => sub { # Set up a Context and save it for creating sockets later: $_[HEAP]->{ctx} = POEx::ZMQ->context; # Create a ZMQ_ROUTER socket associated with our Context: $_[HEAP]->{rtr} = POEx::ZMQ::Socket->new( context => $_[HEAP]->{ctx}, type => ZMQ_ROUTER, ); # Set up the backend socket and start accepting/emitting events: $_[HEAP]->{rtr}->start; # Bind to a local TCP endpoint: $_[HEAP]->{rtr}->bind( 'tcp://127.0.0.1:1234' ); }, zmq_recv_multipart => sub { # ROUTER got message from REQ; # parts are available as a List::Objects::WithUtils::Array -> my $parts = $_[ARG0]; # ROUTER receives [ IDENTITY, NULL, MSG .. ]: my ($id, undef, $content) = $parts->all; my $response; # ... do work ... # Send a response back: $_[KERNEL]->post( $_[SENDER], send_multipart => $id, '', $response ); }, }, ); POE::Kernel->run;
An asynchronous POE-powered http://www.zeromq.org|ZeroMQ socket.
These objects are event emitters powered by MooX::Role::POE::Emitter. That means they come with flexible event processing / dispatch / multiplexing options. See the MooX::Role::Pluggable and MooX::Role::POE::Emitter documentation for details.
Required; the socket type, as a constant.
See zmq_socket(3) for details on socket types.
See POEx::ZMQ::Constants for a ZeroMQ constant exporter.
The POEx::ZMQ::FFI::Context backend context object.
The POEx::ZMQ::FFI::Socket backend socket object.
Start the emitter and set up the associated socket.
This method must be called to create the backend ZeroMQ socket and start the emitter's POE::Session.
Returns the object.
Stop the emitter; a zmq_close(3) will be issued for the socket and "zsock" will be cleared.
Buffered items are not removed; "get_buffered_items" can be used to retrieve them for feeding to a new socket object's "send" method. See POEx::ZMQ::Buffered.
Returns (a shallow copy of) the List::Objects::WithUtils::Array containing messages currently buffered on the POE component (due to a backend ZeroMQ socket's blocking behavior; see zmq_socket(3)).
This will not return messages queued on the ZeroMQ side.
Each item is a POEx::ZMQ::Buffered object; look there for attribute documentation. These can also be fed back to "send" after retrieval from a dead socket, for example:
$old_socket->stop; # Shut down this socket my $pending = $old_socket->get_buffered_items; $new_socket->send($_) for $pending->all;
Retrieve context option values.
See "get_ctx_opt" in POEx::ZMQ::FFI::Context & zmq_ctx_get(3)
Set context option values.
See "set_ctx_opt" in POEx::ZMQ::FFI::Context & zmq_ctx_set(3)
Get socket option values.
See "get_sock_opt" in POEx::ZMQ::FFI::Socket & zmq_getsockopt(3).
Set socket option values.
See "set_sock_opt" in POEx::ZMQ::FFI::Socket & zmq_setsockopt(3).
$sock->bind( @endpoints );
Call a zmq_bind(3) for one or more specified endpoints.
A "bind_added" event is emitted for each added endpoint.
$sock->unbind( @endpoints );
Call a zmq_unbind(3) for one or more specified endpoints.
A "bind_removed" event is emitted for each removed endpoint.
$sock->connect( @endpoints );
A "connect_added" event is emitted for each added endpoint.
$sock->disconnect( @endpoints );
Call a zmq_disconnect(3) for one or more specified endpoints.
A "disconnect_issued" event is emitted for each removed endpoint.
$sock->send( $msg, $flags );
Send a single-part message (without blocking).
$sock->send_multipart( [ @parts ], $flags ); # For example, a ROUTER sending to $id -> $rtr->send_multipart( [ $id, '', $msg ], $flags );
Send a multi-part message.
These POE events take the same arguments as their object-oriented counterparts documented in "METHODS":
Emitted events are prefixed with the value of the "event_prefix" in MooX::Role::POE::Emitter attribute; by default, zmq_.
zmq_
Emitted when a "bind" is issued for an endpoint; $_[ARG0] is the bound endpoint.
$_[ARG0]
Emitted when a "unbind" is issued for an endpoint; $_[ARG0] is the unbound endpoint.
Emitted when a "connect" is issued for an endpoint; $_[ARG0] is the target endpoint.
Emitted when a "disconnect" is issued for an endpoint; $_[ARG0] is the disconnecting endpoint.
sub zmq_recv { my $msg = $_[ARG0]; $_[KERNEL]->post( $_[SENDER], send => 'bar' ) if $msg eq 'foo'; }
Emitted when a single-part message is received; $_[ARG0] is the message item.
# A ROUTER receiving from REQ, for example: sub zmq_recv_multipart { my $parts = $_[ARG0]; my ($id, undef, $content) = @$parts; my $response = 'bar' if $content eq 'foo'; $_[KERNEL]->post( $_[SENDER], send_multipart => [ $id, '', $response ] ); }
Emitted when a multipart message is received; $_[ARG0] is a List::Objects::WithUtils::Array array-type object containing the message parts.
MooX::Role::POE::Emitter, which in turn consumes MooX::Role::Pluggable.
zmq(7)
zmq_socket(3)
POEx::ZMQ::FFI::Context for details on the ZeroMQ context backend.
POEx::ZMQ::FFI::Socket for details on the ZeroMQ socket backend.
ZMQ::FFI for a loop-agnostic ZeroMQ implementation.
Jon Portnoy <avenj@cobaltirc.org>
Licensed under the same terms as Perl.
To install POEx::ZMQ, copy and paste the appropriate command in to your terminal.
cpanm
cpanm POEx::ZMQ
CPAN shell
perl -MCPAN -e shell install POEx::ZMQ
For more information on module installation, please visit the detailed CPAN module installation guide.