better9
better9

Reputation: 151

Is there a server-side library of HTML5 Server-Sent-Events on node.js?

We can simply write some code to fire an event:

res.send("data: " + data + "\n\n");

But SSE has other features, like Last-Event-ID, keep-alive, and so on.

Upvotes: 2

Views: 234

Answers (1)

Kornel
Kornel

Reputation: 100120

Most SSE features are so simple, that a library would be series of 1-liners:

function send_sse_event(res, data) {
   res.write("data: " + data.replace(/\n/g, "data: ") + "\n\n";
}

function get_last_event_id(req) {
   return req.headers['last-event-id'];
}

function set_sse_reconnection_delay(res, ms) {
   res.write("retry: " + ms);
}

You can use SSE without a library without feeling guilty that you're reinventing the wheel.

Upvotes: 1

Related Questions