What is Server-Sent Events in HTML5 ?
Last Updated :
11 Mar, 2024
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 webpage by a server to the client side is known as one-way messaging or mono-directional. The Server-Sent Events are part of web APIs. In this article, we'll learn about Server-Sent Events, how they work, and examples using HTML5.
Check Browser Support for SSE
Before implementing the SSE, checking the support of the browser is necessary. To check the support, we'll use an if and else statement and run the following code using the EventSource object. The EventSource object is used to receive the events or notifications from the server. Below is the syntax of the object.
Syntax:
if(typeof(EventSource) !== "undefined") {
var source = new EventSource("gfg-file.php");
source.onmessage = function(event) {
document.getElementById("yourResult")
.innerHTML += event.data + "<br>";
}
}
Now, that we know about the EventSource object, it's time to check the browser support for SSE. Use a <script> tag of javascript, to run the code of checking the browser support.
Example to Check Browser Support for SSE:
Example: In this example, we will check browser support for SSE. Now, once the if statement runs, if the browser is sporting the EventSource object, the user will see the Output, and if not then it will show the else part of the statement.
HTML
<!DOCTYPE html>
<html>
<body>
<h1>GeeksforGeeks</h1>
<div id="gfg"></div>
<script type="text/javascript">
if (typeof EventSource !== "undefined") {
alert("Congrats Geek! The browser is supporting SSE.");
} else {
alert("Sorry Geek! The browser is not supporting SSE.");
}
</script>
</body>
</html>
Output:

Sending Automatic events from Server:
Now, to send the notification or events from the server, there is a need for a server that is capable of sending the events to the clients. For the demo, we'll create an ASP file for the purpose.
Below is the ASP file for the example:
// gfg_demo.asp
// code starts from here
<%
Response.ContentType = "text/event-stream"
Response.Expires = -1
Response.Write("data: Current Server time: " & now())
Response.Flush()
%>
In the above ASP code, as per the SSE standard, the ContentType response is set to "text/event-stream" and the output of data will be sent starting always with data. The Flush() method of response will send the data event to the user webpage.
Example to Send Automatic events from Server:
Example: In this example, we will send automatic events from the server.
HTML
<!DOCTYPE html>
<html>
<body>
<h2>GFG getting Server-Sent Events </h2>
<div id="output"></div>
<script>
// Started if statement
if (typeof EventSource !== "undefined") {
// Created event source
var gfgSource = new EventSource("gfg_demo.asp");
gfgSource.onmessage = function (event) {
// Output is shown here
document.getElementById("output")
.innerHTML += event.data + "<br>";
};
} else {
document.getElementById("result").innerHTML =
// If browser does not support SSE
"Sorry Geek, The browser does not support SSE";
}
</script>
</body>
</html>
Output

Similar Reads
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
How to receive server-sent event notifications ?
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
3 min read
What is the Vibration API in HTML5 ?
Vibration is a sensation that triggers curiosity in a user and tends to make them perform a task. For example, in Fitness trackers, the vibration alert reminds user if they have been sitting too long and would potentially make them take a walk or move away from the desk or vibrations in phones or sm
2 min read
Difference between server sent events and Websockets in HTML5
WebSockets WebSockets is a complicated technology that permits real-time interactive bidirectional communication between the client browser and a server. This is accomplished by establishing a standard method for the server to transmit information to the client without first receiving an invitation
4 min read
HTML | isTrusted Event Property
The isTrusted event property is used for checking whether an event is trusted or not. Return Value: It returns True if an event is trusted else it returns false. Syntax : event.isTrusted Below program illustrates the isTrusted event property : Example: To find out if a specific event is trusted or n
1 min read
Server-Sent Events (SSE) in Spring WebFlux
Server-sent events (SSE) are standards that define how servers can start sending data to clients. Once an initial client connection has been established, in the context of Spring WebFlux, SSE allows you to send a stream of updates from the server to the client over HTTP. SSE works over a single HTTP
3 min read
Server Components in Next.js
Server Components in Next.js offer a way to build components that are rendered on the server rather than on the client. This feature enhances performance by reducing the amount of JavaScript sent to the browser and allows for faster initial page loads. In this post, we will explore the Server Compon
4 min read
Servlet - Event and Listener
The term "event" refers to the occurrence of something. An event occurs when the state of an object changes. When these exceptions occur, we can conduct certain crucial actions, such as collecting total and current logged-in users, establishing database tables when deploying the project, building da
8 min read
What are synthetic events in ReactJS ?
In order to work as a cross-browser application, React has created a wrapper same as the native browser in order to avoid creating multiple implementations for multiple methods for multiple browsers, creating common names for all events across browsers. PrerequisitesJavaScript & DOM manupulation
3 min read
HTML | view Event Property
The view Event Property is used for returning a reference to the Window object. When an event occurs it returns the reference of the Window Object. Syntax : event.view Return Value: Returns a reference to the Window object. Below program illustrates the view Event Property: html <!DOCTYPE html
1 min read