How to receive server-sent event notifications ?
Last Updated :
06 Sep, 2022
This is a one-way communication that is made with the server by making a single persistent connection. Initially, the client makes a connection with the server and waits for the trigger of the event by the server and the server triggers the events on the client by sending some messages to the client.
Now, we will learn to get notified from the server through server-sent events (in short SSE)
We have to use two different perspectives for this task, which are:
- Client Side: To start a connection
- Server Side: To accept the client requests
On the client side, we create an HTML page and with EventSource class we make an asynchronous request to the server.
EventSource class opens a persistent connection with the server and also provides a method to listen to the server events
We need to create an instance of this class, like:
Syntax:
var sse = new EventSource("url");
where,
- sse is the EventSource objectÂ
The method used to listen for the server messages is "message".
Syntax:
sse.onmessage = (event)=>{
...
}
On the Server side, I am using express and nodejs to listen for the client requests, but you can prefer your favorite server-side language.
Syntax:
var express = require("express");
var app = express();
app.get('/events', async function(req, res) {
res.set({
'Cache-Control': 'no-cache',
'Content-Type': 'text/event-stream',
'Connection': 'keep-alive',
'Access-Control-Allow-Origin':'*'
});
res.flushHeaders();
res.write('hi');
}
app.listen(3000);
In the above code, we are listening at port 3000 and at some URL-PATH
Make sure you make the connection persistent using the header 'Connection': 'keep-alive' and make the response type as event-stream 'Content-Type': 'text/event-stream'
Browser implements CORS so specify 'Access-Control-Allow-Origin':'*'
using res to send notifications to the client.
Example: In the below example we will connect at "http://localhost:3000/events" and print the server message on the console tab. On the server side, we will listen to the "/events" URL using the express GET method.Â
In the server-side code, we create a function run that registers a listener function, and by using res object we send messages to the EventSource instance.Â
The server sends the message, which is the value of the counter that counts every one second, iteratively.
HTML
<html>
<body>
<script type="text/javascript">
const source = new EventSource('http://localhost:3000/events');
source.addEventListener('message', message => {
console.log('Got', message);
});
</script>
</body>
</html>
Output:
OUTPUT
JavaScript
'use strict';
const express = require('express');
run().catch(err => console.log(err));
async function run() {
const app = express();
app.get('/events', async function (req, res) {
console.log('Got /events');
res.set({
'Cache-Control': 'no-cache',
'Content-Type': 'text/event-stream',
'Connection': 'keep-alive',
'Access-Control-Allow-Origin': '*'
});
res.flushHeaders();
// Tell the client to retry every 10 seconds
// if connectivity is lost
res.write('retry: 1000 \n\n');
let count = 0;
while (true) {
await new Promise(resolve => setTimeout(resolve, 1000));
console.log('Emit', ++count);
// Emit an SSE that contains the current
// 'count' as a string
res.write(`data: ${count}\n\n`);
}
});
await app.listen(3000);
console.log('Listening on port 3000');
}
Output: The output represents the emission of the data which is the counter that counts every second
OUTPUT
Similar Reads
How to send push notification using XMPP Server? In this article, we are going to learn how can we send push notifications using the XMPP server. XMPP stands for extensible Messaging and Presence Protocol and it is a free open-source, cross-platform web server solution stack package. which is easy to install and contains mainly the Apache HTTP Ser
2 min read
What is Server-Sent Events in HTML5 ? Server-Sent events (SSE) provide a seamless way to automatically update web pages without requiring user interaction. These events allow servers to push real-time data to clients over HTTP connections. The updates for any website come via HTTP connections. This way of communication of updating the
3 min read
How To Add Push Notification Feature In ReactJS? Adding a push notification feature to your React application can significantly improve user engagement by delivering timely updates, alerts, or messages even when users are not actively using your app. Push notifications are commonly used for sending updates about new content, promotional offers, or
3 min read
How To Install AWS CLI - Amazon Simple Notification Service (SNS)? Amazon Simple Notification Service (SNS) is used for the Application to Application (A2A) and Application to Person (A2P) communication. It provides developers with a highly scalable, flexible, and cost-effective capability to publish messages from an application and immediately deliver them to subs
2 min read
Server - Sent Events in Spring When we design web applications, we generally have a client and a server model, where the client is always the initiator of the transaction or a request to the server. The server responds to these requests. However, in certain scenarios, there arises a need for the server to respond much more freque
5 min read