Server-sent events for Node.js
Send data to the clients in real time with SSE technology
Install it with:
npm install sse-node
or
git clone https://github.com/manuel-di-iorio/sse-node.git
Example with HTTP server:
const SSE = http = ; http;
Example with Express.js:
const SSE = app = ; app; app;
On the client side, just connect with javascript to the server with:
var es = "/sse";es { ; //will output 'Hello world!'};
API
Wrap a HTTP request/response object with this function to create a SSE connection for the incoming request.
*Options* can have the following properties:
`ping`: When to send a ping to the client each X seconds (by default is disabled)
`retry`: After how many millisecons to retry the connection (by default is 3000)
`padding`: Some older browsers require to write a 2KB padding when the SSE connection starts.
By default this is disabled.
This function returns a object with the following methods:
*Data* can be anything. Objects will get serialized
*Event*: You can send a message to a specific event and listen to it on the client side
with ev.addEventListener("eventName", function). By default, all messages are sent to the 'message' event
When the client disconnects, will call the specified callback. The param is optional because this function
also clears the ping interval when set.
Test with:
node test
Then navigate with your browser to http://localhost:9090
and open the javascript console