Send messages to the client in real time using NodeJS and Server-Sent Events

shcheglov

Viktor Shcheglov

Posted on August 1, 2024

Send messages to the client in real time using NodeJS and Server-Sent Events

Overview
Server-Sent Events(SSE) technology, which allows sending information from server to client in real time, is based on HTTP protocol.

On the client side server-sent events API provides EventSource interface (part of HTML5 standard), through which a persistent connection to HTTP server is opened.

The HTTP server sends events in text/event-stream format. The connection remains open until the EventSource.close() method is called.

Limitations:

Only receiving data from the server is possible (unidirectional data stream, unlike WebSockets);
Data can only be transmitted in UTF-8 format (non-binary data).
Possible advantages:

Works over HTTP, which means that clients will not have connection problems when connected via proxies that do not support other connections (such as WebSockets);
If the connection is established via HTTPS, SSE traffic is protected by encryption.
Browser support: https://caniuse.com/eventsource.

In this paper, we will develop a Todo List application that allows users to add, delete, mark as completed tasks in a list.

Note, the state of the list using Server-sent Events will be shared by all users:

💖 💪 🙅 🚩
shcheglov
Viktor Shcheglov

Posted on August 1, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related