logo Learn HTML
  • Home
  • Tutorials
  • Practice
  • Quiz
  • Projects
  • About
  • Contact

HTML Course

  • HTML HOME
  • HTML Introduction
  • HTML Basic
  • HTML Element
  • HTML Attributes
  • HTML Headings
  • HTML Paragrapha
  • HTML Style
  • HTML Formattin
  • HTML Quotations
  • HTML Colors
  • HTML Css
  • HTML Links
  • HTML Image
  • HTML Favicon
  • Page Tittle
  • HTML Tables
  • HTML Lists
  • Block & Inline
  • HTML Div
  • HTML Classes
  • HTML Id
  • HTML Buttons
  • HTML Iframes
  • HTML File Paths
  • HTML Head
  • HTML Layout
  • HTML Symbols
  • HTML Emojis
  • HTML URL Encode
  • HTML VS XHTML
  • HTML FROM
  • HTML Form
  • Form Attributes
  • Form Elements
  • HTML Input Types
  • HTML Input Attributes
  • Input Form Attributes
  • HTML Graphics
  • HTML Canvas
  • HTML SVG
  • HTML Media
  • HTML Media
  • HTML Video
  • HTML Audio
  • HTML Plug-ins
  • HTML Youtube
  • HTML APIS
  • HTML Web APIS
  • HTML Geolocation
  • HTML Drag & Drop
  • Web Storage
  • Web Workers
  • HTML SSE
  • HTML Examples
  • HTML Examples
  • HTML Editor
  • HTML Quiz
  • HTML Exercises
  • HTML References
  • HTML Tag List
  • HTML Attributes
  • HTML Global Attributes
  • HTML Brower Support
  • HTML Events
  • HTML Colors
  • HTML Canvas
  • HTML Audio & Video
  • HTML Doctypes
  • HTML Character Sets
  • HTML URL Encode
  • Language Codes
  • HTTP Messages
  • HTTP Methods
  • PX to EM Converter
  • keyboard Shortcuts

HTML Server-Sent Events (SSE)

Traditionally, a web page must ask a server for data (polling). With the Server-Sent Events (SSE) API, the relationship changes: the server "pushes" updates to the web page automatically as soon as they happen.


1. What are Server-Sent Events?

SSE is a one-way messaging system. Data flows from the server to the browser. This is perfect for apps that need live updates without user interaction, such as:

  • Live stock market tickers.

  • Real-time news feeds.

  • Sports scores and play-by-play updates.

  • Social media notifications.


2. Client-Side Implementation

To receive updates, we use the JavaScript EventSource object.


Step A: Browser Support Check

Not all legacy browsers support SSE, so always check for compatibility first.

Javascript

if (typeof(EventSource) !== "undefined") {

// Support confirmed!

} else {

document.getElementById("result").innerHTML = "Your browser does not support Server-Sent Events.";

}


Step B: Receiving Messages

You define a source URL and listen for the onmessage event.

Example


// Connect to the server-side script

const source = new EventSource("updates.php");


// Triggered every time the server sends new data

source.onmessage = function(event) {

document.getElementById("result").innerHTML += event.data + "<br>";

};


3. Server-Side Requirements

For SSE to work, the server must keep the connection open and send data in a specific format. The most important part is setting the Content-Type header to text/event-stream.


PHP Example (updates.php):

PHP


<?php

header('Content-Type: text/event-stream');

header('Cache-Control: no-cache'); // Prevent the browser from caching old data


// Get the current server time

$currentTime = date('h:i:s A');


// Every message MUST start with "data: " and end with two newlines "\n\n"

echo "data: Current Server Time: {$currentTime}\n\n";


// Push the data to the client immediately

flush();

?>


4. The EventSource Object: Events

Beyond just receiving messages, the EventSource object allows you to track the status of the connection.


Event Description
onopen Triggered when the connection to the server is successfully established
onmessage Triggered when the server sends a new data packet.
onerror Triggered if the connection drops or an error occurs.

5. Browser Compatibility


API Chrome Edge Firefox Safari Opera
SSE 6.0+ 79.0+ 6.0+ 5.0+ 11.5+

Key Takeaways for your Website


  • One-Way only:If you need two-way communication (client to server AND server to client), consider using WebSockets.

  • Automatic Reconnection:If the connection is lost, the browser will automatically try to reconnect to the server.

  • Format Matters:Always ensure your server output starts with data: followed by your message.

Try It Your Self

Sign Up For New
Update

HTML Basic
  1. HTML Basic
  2. HTML Element
  3. HTML Attributes
  4. HTML Headings
  5. HTML Paragrapha
  6. HTML Tables
  7. HTML Lists
HTML Advance
  1. HTML Div
  2. HTML Classes
  3. HTML Id
  4. HTML Buttons
  5. HTML Iframes
  6. HTML Symbols
  7. HTML Emojis