HTTP::ServerEvent - create strings for HTTP Server Sent Events
->as_string( %options )
return HTTP::ServerEvent->as_string( event => "ping", data => time(), retry => 5000, # retry in 5 seconds if disconnected id => $counter++, );
Returns a string that can be sent as a server-sent event through the socket.
The allowed options are:
event - the type of event (optional). This is the event type you will want to listen to on the other side. Newlines or null characters in the event type are treated as a fatal error.
event
data - the data to be sent. This can be either a string or an array reference of strings. Note that embedded newlines (either \x{0d} , \x{0a} or \x{0d}\x{0a} ) will be interpreted as newlines and be normalized to the \x{0d}\x{0a} pairs that are sent over the wire.
data
\x{0d}
\x{0a}
\x{0d}\x{0a}
id - the event id. If you send this, a client will send the Last-Event-Id header when reconnecting, allowing you to send the events missed while offline. Newlines or null characters in the event id are treated as a fatal error.
id
Last-Event-Id
retry - the amount of miliseconds to wait before reconnecting if the connection is lost. Newlines or null characters in the retry interval are treated as a fatal error.
retry
To receive events on the other side, usually in a browser, you will want to instantiate an EventSource object.
EventSource
var events = new EventSource('/events'); // Subscribe to "tick" event events.addEventListener('tick', function(event) { var out= document.getElementById("my_console"); out.appendChild(document.createTextNode(event.data)); }, false);
If you're sending events, you may want to look at the Last-Event-Id HTTP header. This header is sent by the EventSource object when reestablishing a connection that was intermittently lost. You can use this to bring the reconnecting client up to date with the current state instead of transmitting the complete state.
https://developer.mozilla.org/en-US/docs/Server-sent_events/Using_server-sent_events
https://hacks.mozilla.org/2011/06/a-wall-powered-by-eventsource-and-server-sent-events/
http://www.html5rocks.com/en/tutorials/eventsource/basics/?ModPagespeed=noscript
The public repository of this module is http://github.com/Corion/http-serverevent.
The public support forum of this module is http://perlmonks.org/.
Please report bugs in this module via the RT CPAN bug queue at https://rt.cpan.org/Public/Dist/Display.html?Name=HTTP-ServerEvent or via mail to http-serverevent-Bugs@rt.cpan.org.
Max Maischein corion@cpan.org
corion@cpan.org
Copyright 2013-2013 by Max Maischein corion@cpan.org.
This module is released under the same terms as Perl itself.
To install HTTP::ServerEvent, copy and paste the appropriate command in to your terminal.
cpanm
cpanm HTTP::ServerEvent
CPAN shell
perl -MCPAN -e shell install HTTP::ServerEvent
For more information on module installation, please visit the detailed CPAN module installation guide.