<div dir="ltr">Dear all,<div><br></div><div>I'm trying to write a Dancer2::Plugin to maintain ZMQ socket and send some information to a backend when a request is received.</div><div><br></div><div>This is the plugin :</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">







<p class="gmail-p1">package Dancer2::Plugin::ZMQ;</p><p class="gmail-p1"><br></p><p class="gmail-p1">use Dancer2::Plugin;</p><p class="gmail-p1"><br></p><p class="gmail-p1">use ZMQ::FFI;</p><p class="gmail-p1">use ZMQ::FFI::Constants qw(ZMQ_REQ);</p><p class="gmail-p1"><br></p><p class="gmail-p1">has host => (is => 'ro');</p><p class="gmail-p1">has identity => (is => 'ro');</p><p class="gmail-p1"><br></p><p class="gmail-p1">has _context => (is => 'ro', lazy_build => 1);</p><p class="gmail-p1">sub _build_context {</p><p class="gmail-p1">    my $plugin = shift;</p><p class="gmail-p1">    return ZMQ::FFI->new();</p><p class="gmail-p1">}</p><p class="gmail-p1"><br></p><p class="gmail-p1">has client => (is => 'ro', lazy_build => 1, plugin_keyword => 'zmq');</p><p class="gmail-p1">sub _build_client {</p><p class="gmail-p1">    my $plugin = shift;</p><p class="gmail-p1"><br></p><p class="gmail-p1">    my $client = $plugin->_context->socket(ZMQ_REQ);</p><p class="gmail-p1">    $client->set_identity = $plugin->config->{identity} . "-$$";</p><p class="gmail-p1">    $client->connect($plugin->config->{host});</p><p class="gmail-p1">    return $client;</p><p class="gmail-p1">}</p><p class="gmail-p1"><br></p><p class="gmail-p1">1;</p></blockquote><div> </div><div> This is my config.yml:</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">plugins:<br>    ZMQ:<br>        identity: "api-gw"<br>        host: "tcp://proxy:6660"</blockquote><div><br></div><div><br></div><div>And this is how I use it in one route:</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">post '/' => sub {</blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">    my $message = body_parameters->get('message');</blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">    zmq->send(message =>$message);</blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">    my $reply = zmq->recv();</blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">    return {"reply" => $reply};</blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">};</blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> </blockquote><div><br></div><div>The issue is that I get this error:</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Router exception: Can't call method "send" on an undefined </blockquote><div> <br></div><div>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.</div><div><br></div><div>And before someone asks, the backend is working and replying. A simple script like this one works in my setup:</div><div><br></div><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">use v5.10;<br>use ZMQ::FFI qw(ZMQ_REQ);<br></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> </blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">my $endpoint = "tcp://proxy:6660";<br>my $ctx      = ZMQ::FFI->new();<br></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> </blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">my $s1 = $ctx->socket(ZMQ_REQ);<br>$s1->connect($endpoint);<br></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> </blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">$s1->send('ohhai');<br>print $s1->recv();</blockquote><div><br></div><div>Thanks in advance.</div><div>Regards,</div><div>Alfonso </div></div></div>