Science Knowings: HTML Course For Social Media

HTML5 Server-Sent Events

What are Server-Sent Events?

Server-Sent Events (SSE) is a technology that allows a server to push data to a client web browser over a persistent HTTP connection.

How do Server-Sent Events work?

SSE uses a long-lived HTTP connection, which is kept open by the server. The server can then send data to the client whenever it has new information available.

Benefits of Server-Sent Events

Benefits of Server-Sent Events

  1. Real-time data updates
  2. Reduced server load
  3. Improved battery life on mobile devices

Creating an SSE Server

To create an SSE server, you need to use a compatible web server, such as Apache or Nginx. You will also need to write a script that generates the SSE data.

Creating an SSE Client

To create an SSE client, you need to use the EventSource object. The EventSource object is a JavaScript interface that allows you to listen for SSE events.

EventSource Object

The EventSource object has a number of properties and methods that you can use to listen for and handle SSE events.


  • readyState
  • url
  • withCredentials
  • addEventListener()
  • removeEventListener()
  • close()

EventSource Methods

The EventSource object has three methods that you can use to listen for and handle SSE events:

  • addEventListener()
  • removeEventListener()
  • close()

EventSource Properties

The EventSource object has three properties that you can use to get information about the SSE connection:

  • readyState
  • url
  • withCredentials

EventSource Events

The EventSource object fires three events that you can listen for:

  • open
  • message
  • error

Handling Server-Sent Events

let eventSource = new EventSource("sse.php");
eventSource.addEventListener("message", function(e) {

Using Server-Sent Events for Real-Time Data

SSE is ideal for applications that need to receive real-time data, such as stock prices, news updates, or social media feeds.

Next Topic: HTML5 History API

In the next session, we'll explore the HTML5 History API, which allows you to manipulate the browser's history and navigation. Follow us to learn more!