Using Server Sent Events (SSE) with Session

SSE or server sent events are a great choice for pushing data from server to the clients. The benefit of SSE over web-sockets is that unlike web-sockets SSE is a one-way communication which sends data to client when a change in data happens in the server without the client explicitly asks for new information.

This will drastically reduce the traffic and more over, as SSE is working with regular http requests, there is no need of separate port which is usually blocked by firewalls.

The SSE is mainly used to send out general information, like stock information which is common to all the clients. But this can also be used to send out customized data for each of the clients.

To send out customized data, we need to get the identification of the connected client by an id through the URL.

We have implemented SSE using NodeJS.

Then the two main packages that help us are:



After setting up these two packages properly, all the variables that are being used for gathering and sending data should also be set in the session scope like:

req.session.userid = “abcd”

Now, the data that is sent from SSE will be based on the userid.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.