package Dancer2::Plugin::ZMQ;
use Dancer2::Plugin;
use ZMQ::FFI;
use ZMQ::FFI::Constants qw(ZMQ_REQ);
has host => (is => 'ro');
has identity => (is => 'ro');
has _context => (is => 'ro', lazy_build => 1);
sub _build_context {
my $plugin = shift;
return ZMQ::FFI->new();
}
has client => (is => 'ro', lazy_build => 1, plugin_keyword => 'zmq');
sub _build_client {
my $plugin = shift;
my $client = $plugin->_context->socket(ZMQ_REQ);
$client->set_identity = $plugin->config->{identity} . "-$$";
$client->connect($plugin->config->{host});
return $client;
}
1;
plugins:
ZMQ:
identity: "api-gw"
host: "tcp://proxy:6660"
post '/' => sub {
my $message = body_parameters->get('message');
zmq->send(message =>$message);
my $reply = zmq->recv();
return {"reply" => $reply};
};
Router exception: Can't call method "send" on an undefined
use v5.10;
use ZMQ::FFI qw(ZMQ_REQ);
my $endpoint = "tcp://proxy:6660";
my $ctx = ZMQ::FFI->new();
my $s1 = $ctx->socket(ZMQ_REQ);
$s1->connect($endpoint);
$s1->send('ohhai');
print $s1->recv();