2. SSE - EventSource API
Server-Sent Events (SSE) is a HTML5 standard
describing how servers can initiate data
transmission towards clients once an initial
client connection has been established.
Browsers: Firefox, Chrome, Opera, Safari
3. SSE != Websockets
⢠SSE connections can only push data to the
browser.
⢠Itâs still HTTP - no new protocol
⢠Automatic reconnection
5. SSE â on the frontend
function newStream(){
var source = new EventSource('/streaming');
source.addEventListener('update', function(e) {
//do stuff
});
}
6. SSE â on the frontend
function newStream(){
var source = new EventSource('/streaming');
source.onmessage (function(e) {
//do stuff
});
}
7. EventSource Object Methods
onopen -When a connection to the server is
opened
onmessage - When a message is received
onerror - When an error occurs
8. SSE â message format
Sample:
event: my_event
data: {"username": âPeter"}
event - The event's type. If this is specified, an event will be
dispatched on the browser to the listener for the specified event
name.
data - field for the message.
id - the event ID to set the EventSource object's last event ID value
retry - The reconnection time to use when attempting to send the
event.
9. Rails 4 - ActionController::Live
Live Streaming: the ability to send partial
responses to the client immediately.
10. What about the server?
Server must support:
⢠Long running requests
⢠Live streaming
⢠Concurrency
⢠Examples: Puma, Rainbows, Thin