Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
**UPDATE: The issue has been resolved. I've preserved the original post, but added the final, working code to the bottom. I sincerely hope this benefits others!**

I'm very new to event-based programming in Perl. I'm currently trying to get to grips with a Plack / Twiggy / AnyEvent / SockJS combination (details below). Unfortunately, I'm not doing so well.

I'm trying to write a program that will send the client a message whenever a certain event happens (in the production version, the event will be a database update, but for the purposes of this test, I'm trying to use an AnyEvent timer). However, I have no clue from which end to approach this. Here is the code I have so far (this is heavily based on the example code from the SockJS-perl docs):

#!/usr/bin/perl use strict; use warnings; use Plack::Builder; use SockJS; use AnyEvent; builder { mount '/echo' => SockJS->new( handler => sub { my ($session) = @_; $session->on( 'data' => sub { my $session = shift; $session->write('got your message'); } ); $session->write('connected'); my $w_cond = AnyEvent->condvar; my $w; $w = AnyEvent->timer( after => 0, interval => 5, cb => sub { undef $w; # cleanup $session->write('5 seconds have passed'); $w_cond->send(); } ); $w_cond->recv; }; ); };

This code is executed with:

plackup --server Twiggy::TLS --port 5000 --tks-key /path/to/key --tls-cert /path/to/cert app.psgi

This code doesn't work! Console.log outputs the following:

open connected close

It is obvious that $w->recv is the culprit because if I comment out that line, the server works and outputs 'got your message' when I send a message to it. However, in that case the timer doesn't work.

I know that SockJS is a module in very early development, and I have contacted the author for more information, but I have the feeling that my problem has nothing to do with SockJS, but with the way Plack, Twiggy and AnyEvent work. Hence my posting the question here on perlmonks. Any input / insight would be greatly appreciated (because not understanding this is driving me up the wall!!!)

For completeness, here is the client-side JavaScript code (copied 95% from the example on the SockJS module page):

<script src="http://cdn.sockjs.org/sockjs-0.3.min.js"></script> <script> var sock = new SockJS("https://0.0.0.0:5000/echo"); sock.onopen = function() { console.log("open"); }; sock.onmessage = function(e) { console.log(e.data); }; sock.onclose = function() { console.log("close"); }; </script>

**Update: The issue has been resolved!**

Final, working code:

#!/usr/bin/perl use strict; use warnings; use Plack::Builder; use SockJS; use AnyEvent; builder { mount '/echo' => SockJS->new( handler => sub { my ($session) = @_; $session->on( 'data' => sub { my $session = shift; $session->write('got your message'); } ); $session->write('connected'); my $w_cond = AnyEvent->condvar; $w_cond->cb( sub { warn $_[0]->recv } ); my $w; $w = AnyEvent->timer( after => 0, interval => 5, cb => sub { undef $w if (0); $session->write('5 seconds have passed'); $w_cond->send(); } ); }; ); };

In reply to PSGI, Plack, Twiggy, AnyEvent and SockJS... I need help by xtpu2

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (8)
    As of 2014-10-20 04:22 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (72 votes), past polls