Skip to content

Instantly share code, notes, and snippets.

@yaxar
Created December 10, 2011 03:16
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 yaxar/1454455 to your computer and use it in GitHub Desktop.
Save yaxar/1454455 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use Mojolicious::Lite;
use EV;
use AnyEvent::IRC::Client;
# Join #mojo on irc.perl.org
my $irc = AnyEvent::IRC::Client->new;
$irc->connect('irc.perl.org', 6667, {nick => "mojobot$$"});
$irc->send_srv(JOIN => '#test');
get '/' => 'index';
get '/events' => sub {
my $self = shift;
# Emit "msg" event for every new IRC message
$self->res->headers->content_type('text/event-stream');
my $g = $irc->reg_cb(publicmsg => sub {
my $message = pop->{params}->[1];
$self->write("event:msg\ndata: $message\n\n");
});
$self->on(finish => sub { undef $g });
};
app->start;
__DATA__
@@ index.html.ep
<!doctype html><html>
<head>
<title>The Mojolicious IRC channel</title>
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script>
var events = new EventSource('<%= url_for 'events' %>');
// Subscribe to "msg" event
events.addEventListener('msg', function(event) {
$("#currentcontent").append(document.createTextNode(event.data));
$("#currentcontent").append('<br>');
}, false);
</script>
</head>
<body>
<div id="currentcontent" style=min-width: 200px; max-width: 500px; margin-left: auto; margin-right: auto;"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment