Dear all, I'm trying to write a Dancer2::Plugin to maintain ZMQ socket and send some information to a backend when a request is received. This is the plugin :
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;
This is my config.yml: plugins:
ZMQ: identity: "api-gw" host: "tcp://proxy:6660"
And this is how I use it in one route: post '/' => sub { my $message = body_parameters->get('message'); zmq->send(message =>$message); my $reply = zmq->recv(); return {"reply" => $reply}; }; The issue is that I get this error:
Router exception: Can't call method "send" on an undefined
I don't understand why it's not keeping the client instantiated. What is wrong withmy approach? What I don't want is to create a socket for each request received. And before someone asks, the backend is working and replying. A simple script like this one works in my setup: 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();
Thanks in advance. Regards, Alfonso