On Monday, August 8, 2016 at 9:20:09 PM UTC-6, Aaron wrote:
>
> Is there a way to do the database polling in a single loop that then sends 
> the notification out to all connected web socket clients?  Does this need 
> to be set up as an entirely other application that does the polling and 
> then have the web socket route listen for messages from it?  Or am I 
> overcomplicating this horribly?
>

What about something like (untested):

my %clients = ();
Mojo::IOLoop->recurring(5 => sub {
    ## poll the db
    ...

    ## broadcast to all connected clients
    $_->send({text => "updated"}) for values %clients;
});

## routes here
...

When a client connects, store the socket (I believe $ws will be stringified 
as the key):

    $c->on(message => sub { my $ws = shift; $clients{$ws} = $ws })

and when a client disconnects, remove the socket:

    $c->on(finish => sub { my $ws = shift; delete $clients{$ws} });

Scott

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Reply via email to