Member-only story

WebSockets and Server-Sent Events with Node.js and Express

Differences, advantages and disadvantages in two easy ways of streaming data from a server

Marian C.
6 min readFeb 17, 2022

Normally a web page requests data from a server. But sometimes it is needed that the server keeps sending data to a browser whenever it has something to send and not only when when the browser makes a request. For example, if a web page displays a live log of some process on the server. There are two straightforward techniques to achieve that — WebSocket and Server-Sent Events (SSE). In this post I compare the two techniques and demonstrate the simplicity and similarity of their client and server-side code.

To demonstrate the properties of WebSocket and SSE, I prepared a simple sample web page https://express-websocket-sse.glitch.me/ running from a Node.js with Express back end.

When you open the page, you see an iframe with two tables.

The left table displays the contents of WebSocket messages and the right table shows SSE messages. The server sends both types of messages every second. The WebSocket and SSE messages have identical contents but are…

--

--

Marian C.
Marian C.

Written by Marian C.

Java, JavaScript and SQL developer. Interested in data collection and visualization.

Responses (2)