Skip to content

Instantly share code, notes, and snippets.

/pubsub.pl Secret

Created February 14, 2015 01:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/9d6b6d9f049a8ce12931 to your computer and use it in GitHub Desktop.
Save anonymous/9d6b6d9f049a8ce12931 to your computer and use it in GitHub Desktop.
use Mojolicious::Lite;
use Mojo::Pg;
helper pg =>
sub { state $pg = Mojo::Pg->new('postgresql://tester:testing@/test') };
get '/' => sub {
my $c = shift;
my $address = $c->tx->remote_address;
my $port = $c->tx->remote_port;
$c->inactivity_timeout(300);
my $cb = $c->pg->pubsub->listen(visitors => sub {
my ($pubsub, $visitor) = @_;
$c->write($visitor);
});
$c->pg->pubsub->notify(visitors => "$address:$port");
$c->on(finish => sub { shift->pg->pubsub->unlisten(visitors => $cb) });
};
app->start;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment