Server-Sent Events¶
Valum provides a middleware for the HTML5 Server-Sent Events protocol to stream notifications over a persistent connection.
The ServerSentEvents.stream_events
function creates a handling middleware
and provide a send
callback to transmit the actual events.
using Valum;
using Valum.ServerSentEvents;
app.get ("sse", stream_events ((req, send) => {
send (null, "some data");
}));
var eventSource = new EventSource ("/sse");
eventSource.onmessage = function(message) {
console.log (message.data); // displays 'some data'
};
Multi-line messages¶
Multi-line messages are handled correctly by splitting the data into into
multiple data:
chunks.
send (null, "some\ndata");
data: some
data: data