Permalink
e863386 Feb 15, 2015
34 lines (25 sloc) 942 Bytes
use Mojolicious::Lite;
use Mojo::Pg;
helper pg => sub { state $pg = Mojo::Pg->new('postgresql://postgres@/test') };
get '/' => 'chat';
websocket '/channel' => sub {
my $c = shift;
$c->inactivity_timeout(3600);
# Forward messages from the browser to PostgreSQL
$c->on(message => sub { shift->pg->pubsub->notify(mojochat => shift) });
# Forward messages from PostgreSQL to the browser
my $cb = $c->pg->pubsub->listen(mojochat => sub { $c->send(pop) });
$c->on(finish => sub { shift->pg->pubsub->unlisten(mojochat => $cb) });
};
app->start;
__DATA__
@@ chat.html.ep
<form onsubmit="sendChat(this.children[0]); return false"><input></form>
<div id="log"></div>
<script>
var ws = new WebSocket('<%= url_for('channel')->to_abs %>');
ws.onmessage = function (e) {
document.getElementById('log').innerHTML += '<p>' + e.data + '</p>';
};
function sendChat(input) { ws.send(input.value); input.value = '' }
</script>