DEV Community

Viktor Shcheglov
Viktor Shcheglov

Posted on

1

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:

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay