Widely available Supported across all major browsers. Safe to use in production.

Browser support

Feature Desktop Mobile
Chrome
Edge
Firefox
Safari
Chrome Android
Safari iOS
6
79
6
5
18
5

The close() method of the EventSource interface closes the connection, if one is made, and sets the EventSource.readyState attribute to 2 (closed).

6
79
6
5
18
5

The error event of the EventSource API is fired when a connection with an event source fails to be opened.

6
79
6
5
18
5

The EventSource() constructor returns a newly-created EventSource, which represents a remote resource.

6
79
6
5
18
5
EventSource (options withCredentials parameter)

`options.withCredentials` parameter

26
79
11
7
26
7

The message event of the EventSource interface is fired when data is received through an event source.

6
79
6
5
18
5

The open event of the EventSource interface is fired when a connection with an event source is opened.

6
79
6
5
18
5

The readyState read-only property of the EventSource interface returns a number representing the state of the connection.

6
79
6
5
18
5

The url read-only property of the EventSource interface returns a string representing the URL of the source.

18
79
6
6
18
6

The withCredentials read-only property of the EventSource interface returns a boolean value indicating whether the EventSource object was instantiated with CORS credentials set.

26
79
6
7
26
7
worker_support

Available in workers

6
79
133
5
18
5
1+Supported (version) Not supported Has note Sub-feature descriptions sourced from MDN Web Docs (CC BY-SA 2.5)
Notes 3 item(s)
Limitation
  • This browser only partially implements this feature
Removed
  • This feature was removed in a later browser version (133)
Implementation note
  • Not supported in service workers.

Syntax

JAVASCRIPT
const evtSource = new EventSource("sse.php");
const eventList = document.querySelector("ul");

evtSource.onmessage = (e) => {
  const newElement = document.createElement("li");

  newElement.textContent = `message: ${e.data}`;
  eventList.appendChild(newElement);
};

Use cases

  • Using Server-sent events

    The EventSource interface is web content's interface to server-sent events.

Cautions

  • May not be supported in older browsers.

Implementation notes

  • Some APIs require a secure context (HTTPS) or user activation. Check the requirements before use.