NAME
ZMQ::Serializer - Enable Serializers
SYNOPSIS
use ZMQ qw(:all);
use ZMQ::Serializer;
my $ctxt = ZMQ::Context->new;
my $socket = $ctxt->socket( ZMQ_REQ );
$socket->send_as( json => { foo => 1 } );
my $hash = $socket->recv_as( 'json' );
DESCRIPTION
You can add a simple serialization/deserialization mechanism to ZMQ by enabling this module.
To enable serialization, you must load ZMQ::Serializer:
use ZMQ;
use ZMQ::Serializer;
This will add ZMQ::Socket::sendmsg_as()
and ZMQ::Socket::recvmsg_as
methods.
You also need to tell it how/what to serialize/deserialize. To do this, use register_write_type()
to register a name and an associated callback to serialize the data. For example, for JSON we do the following (this is already done for you in ZMQ.pm if you have JSON.pm installed):
use JSON ();
ZMQ::Serializer::register_write_type('json' => \&JSON::encode_json);
ZMQ::Serializer::register_read_type('json' => \&JSON::decode_json);
Then you can use sendmsg_as()
and recvmsg_as()
to specify the serialization type as the first argument:
my $ctxt = ZMQ::Context->new();
my $sock = $ctxt->socket( ZMQ_REQ );
$sock->sendmsg_as( json => $complex_perl_data_structure );
The otherside will receive a JSON encoded data. The receivind side can be written as:
my $ctxt = ZMQ::Context->new();
my $sock = $ctxt->socket( ZMQ_REP );
my $complex_perl_data_structure = $sock->recvmsg_as( 'json' );
No serializers are loaded by default. Look for ZMQ::Serializer::* namespace in CPAN.
FUNCTIONS
register_read_type($name, \&callback)
Register a read callback for a given $name
. This is used in recvmsg_as()
. The callback receives the data received from the socket.
register_write_type($name, \&callback)
Register a write callback for a given $name
. This is used in sendmsg_as()
The callback receives the Perl structure given to sendmsg_as()