Server-Sent Events(SSE) Tutorials

For receiving data from the server to the client we generally establish an HTTP connection. In case of getting a continuous update, we would previously(in some cases) use Polling or Long Polling on an HTTP request.

To have a persistent connection we use WebSockets so that we can send/receive data continuously without making a new connection.

Though WebSockets are excellent ways to communicate to servers when we need to both send and receive data to and from the server. But in lots of cases, we need to receive data from the server using a persistent connection, but there is no need to send data to the server. Like, a live dashboard, or a live analytics board. In these cases, WebSockets become an unnecessary overhead.

To solve this problem, a new way is introduced named Server-Sent Events(SSE).

Here is the list of articles on SSE that will give you complete knowledge about SSE:

SSE Basic

Server-Sent Events(SSE) Details

This article discusses all the details about SSE, all the headers, and options. You will get a clear idea about SSE requests/responses and all the use cases.

SSE Clients

Web (JS EventSource) Client

This article is about SSE web client EventSource. All the details about EventSource are discussed here. Also, the options available are discussed based on the implementation. This article will give you a clear idea about how to implement an SSE consumer on the frontend.

Backend Implementations

NodeJS Implementation

This article describes how to implement a complete API backend for SSE in NodeJS (using the ExpressJS framework). Endpoints for receiving and sending messages are implemented. Also, a list of event subscribers is maintained in this implementation.

PHP Implementation

This article explains the implementation of SSE in PHP. We have discussed how to fetch data from data sources (line database, file, 3rd party API, etc.) and send it to the SSE subscriber. In the end, there is an implementation of Redis Pub/Sub so that the PHP script can receive and send events independently.

Go Implementation

This article explains the implementation of SSE in Go(Golang). We have discussed using Go channels and concurrency to implement the message receiving and sending to the SSE subscriber.

Flask (Python) Implementation

This article explains the implementation of SSE in Python using Flask. 2 ways of implementing SSE are discussed in this article.

Leave a Comment