<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">I do a very similar thing for a custom redis plugin I wrote<div class=""><br class=""></div><div class="">You have to declare the object outside the BUILD and set it inside</div><div class=""><br class=""></div><div class="">eg</div><div class="">package Dredis;</div><div class=""><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">use Redis::Fast;</span></div></div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">use Dancer2::Plugin;</span></div><div style="margin: 0px; font-size: 10px; line-height: normal; font-family: Monaco; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class=""><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">my $redii;</span></div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">sub BUILD {</span></div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> my ($plugin) = @_;</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre"> </span><snip lots of setup stuff></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre"> </span>$redii->{$r}{$i} = Redis::Fast->new( %opts );</span></div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">Then use $redi-> in the get/set methods</span></div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">eg</span></div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><font face="Monaco" style="font-size: 10px;" class="">s</font>ub dredis {</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> my( $called, $plugin, $method, @args ) = @_;</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> return $redii->{$called}->$method(@args);</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">}</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">1;</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><br class=""></span></div></span></div></span></div><div class=""><div class="">
--<br class="">Clive Eisen<br class="">GPG: 75056DD0<br class=""><br class=""><br class=""><br class=""><br class=""><br class="">
</div>
<br class=""><div><blockquote type="cite" class=""><div class="">On 13 Oct 2017, at 14:29, Alfonso Pinto <<a href="mailto:alfonso.pinto@gmail.com" class="">alfonso.pinto@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Dear all,<div class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class="">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 class=""></p><p class="gmail-p1">use Dancer2::Plugin;</p><p class="gmail-p1"><br class=""></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 class=""></p><p class="gmail-p1">has host => (is => 'ro');</p><p class="gmail-p1">has identity => (is => 'ro');</p><p class="gmail-p1"><br class=""></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 class=""></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 class=""></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 class=""></p><p class="gmail-p1">1;</p></blockquote><div class=""> </div><div class=""> This is my config.yml:</div><div class=""><br class=""></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 class=""> ZMQ:<br class=""> identity: "api-gw"<br class=""> host: "<a href="tcp://proxy:6660" class="">tcp://proxy:6660</a>"</blockquote><div class=""><br class=""></div><div class=""><br class=""></div><div class="">And this is how I use it in one route:</div><div class=""><br class=""></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 class=""><br class=""></div><div class="">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 class=""> <br class=""></div><div class="">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 class=""><br class=""></div><div class="">And before someone asks, the backend is working and replying. A simple script like this one works in my setup:</div><div class=""><br class=""></div><div class=""><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 class="">use ZMQ::FFI qw(ZMQ_REQ);<br class=""></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 = "<a href="tcp://proxy:6660" class="">tcp://proxy:6660</a>";<br class="">my $ctx = ZMQ::FFI->new();<br class=""></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 class="">$s1->connect($endpoint);<br class=""></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 class="">print $s1->recv();</blockquote><div class=""><br class=""></div><div class="">Thanks in advance.</div><div class="">Regards,</div><div class="">Alfonso </div></div></div>
_______________________________________________<br class="">dancer-users mailing list<br class=""><a href="mailto:dancer-users@dancer.pm" class="">dancer-users@dancer.pm</a><br class="">http://lists.preshweb.co.uk/mailman/listinfo/dancer-users<br class=""></div></blockquote></div><br class=""></div></div></body></html>