Understanding Server-Sent Events With Node.js
In this article let’s build a simple node.js data streaming endpoint with a technique called server-sent events (SSE). We will see how we could write a simple SSE endpoint without any external libraries (not even Express.js) but only using the core modules of Node.js.
We will also understand What SSE actually is, in what scenarios we could use it and how it is different from WebSockets and we will also look into some limitations that come with using the SSE technique.
So, without any further ado, let’s jump right in.
Background:
Server sent event is a mechanism for a server to send updates to its clients. By updates, we mean new information as and when it is available. The important thing to be aware of here is that SSE is unidirectional. The data direction of data flow is only from server to client. The other way is not possible.
If you require a bidirectional stream, then web sockets will be the best approach to go about doing things. In this article, we will only focus on SSE.
Let’s briefly look at the two rudimentary approaches, which paved the way for SSE:
Short Polling:
The most rudimentary and inefficient way to check if a server has new information is to short-poll…