Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: PSGI, Plack, Twiggy, AnyEvent and SockJS... I need help

by kcott (Abbot)
on Apr 09, 2014 at 02:09 UTC ( #1081592=note: print w/ replies, xml ) Need Help??


in reply to PSGI, Plack, Twiggy, AnyEvent and SockJS... I need help

G'day xtpu2,

[Disclaimer: I haven't used AnyEvent previously. The following information was gleaned from its (and AnyEvent::Intro's) documentation.]

"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."

This may only be part of the story, but it might help resolve the AnyEvent issue.

You're calling recv() on a time watcher when it should be called on a condition variable. Also, you'll need a send() method in your timer callback.

Here's a minimal example intended to roughly mirror the AnyEvent part of the code you posted:

#!/usr/bin/env perl -l use strict; use warnings; use AnyEvent; use AnyEvent::Strict; print 'Start at: ', scalar localtime; my $w_cond = AnyEvent->condvar; my $w = AnyEvent->timer( after => 5, cb => sub { print 'Callback: ', scalar localtime; $w_cond->send; } ); $w_cond->recv;

Output:

Start at: Wed Apr 9 12:06:50 2014 Callback: Wed Apr 9 12:06:55 2014

-- Ken


Comment on Re: PSGI, Plack, Twiggy, AnyEvent and SockJS... I need help
Select or Download Code
Re^2: PSGI, Plack, Twiggy, AnyEvent and SockJS... I need help
by xtpu2 (Acolyte) on Apr 09, 2014 at 02:38 UTC

    Thanks for those tips! I have modified the code accordingly and modified the OP. Unfortunately, the behavior of my code is the same (while your code snippet works correctly).

      I guess, the module you create is already called from some event-driven system. In other words, your module is a "callback". The "callback" can't run it's own event distribution. You can register one more "callback" with the main system and then simply return control to the caller. The main system is then responsible for activating your new callback at appropriate time.

      The AnyEvent module is event-distribution system. When you call $w->recv you activate this system, which blocks the main one, which in turn might trigger some time-out for killing your callback. Figure out how to add timers to the main framework. I don't know Plack and don't have any desire to study it, so I can't help you with this, sorry.

      At the core, event-driven programming is very simple. The main part is manager looping over all possible events. When event is encountered, the manager checks if there are any "callbacks" waiting for this event. Those callbacks are activated and the looping continues. The callbacks must finish their work quickly and if necessary, they can register more callbacks for different events.

      Everything else, is specific ways, how different managers keep track of callbacks and events.

        At least from looking at the SockJS documentation, it is based on ( or compatible with) AnyEvent.

        Wow, I think you've nailed it! After reading your post, I started digging into that and came across this, discussing this exact issue:

        https://github.com/miyagawa/Twiggy/issues/33

        I'm posting the modified code here so as not to make the OP confusing. The new code no longer closes the connection immediately, but the timer, for some reason only fires ONCE (the first time), whereas I would like it to keep going. I'm guessing this has something to do with the line undef $w; but when I get rid of it, the timer doesn't fire at all.

        #!/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; # cleanup $session->write('5 seconds have passed'); $w_cond->send(); } ); }; ); };

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1081592]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2014-12-23 03:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (134 votes), past polls