Skip to content

Instantly share code, notes, and snippets.

@furagu
Last active April 8, 2022 19:13
Show Gist options
  • Save furagu/9090506 to your computer and use it in GitHub Desktop.
Save furagu/9090506 to your computer and use it in GitHub Desktop.
Mojolicious websocket redis publish/subscribe example
#!/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