Last active
April 8, 2022 19:13
-
-
Save furagu/9090506 to your computer and use it in GitHub Desktop.
Mojolicious websocket redis publish/subscribe example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# Usage: | |
# cpanm Mojolicious Mojo::Redis | |
# ./mojo-websocket-pubapp.pl daemon | |
use Mojolicious::Lite; | |
use Mojo::Redis; | |
websocket '/' => sub { | |
my $self = shift; | |
my $pub = Mojo::Redis->new; | |
my $sub = $pub->subscribe('test'); | |
$sub->on(message => sub { | |
my ($sub, $message, $channel) = @_; | |
$self->send($message . ' ' . time); | |
}); | |
$self->on(finish => sub { | |
undef $self; | |
undef $pub; | |
undef $sub; | |
}); | |
}; | |
app->start; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment