Back to Content

EventSource: error event

files/en-us/web/api/eventsource/error_event/index.md

latest1.1 KB
Original Source

{{APIRef("Server Sent Events")}}{{AvailableInWorkers}}

The error event of the {{domxref("EventSource")}} API is fired when a connection with an event source fails to be opened.

This event is not cancelable and does not bubble.

Syntax

Use the event name in methods like {{domxref("EventTarget.addEventListener", "addEventListener()")}}, or set an event handler property.

js-nolint
addEventListener("error", (event) => { })

onerror = (event) => { }

Event type

A generic {{domxref("Event")}}.

Examples

js
const evtSource = new EventSource("sse.php");

// addEventListener version
evtSource.addEventListener("error", (e) => {
  console.log("An error occurred while attempting to connect.");
});

// onerror version
evtSource.onerror = (e) => {
  console.log("An error occurred while attempting to connect.");
};

Specifications

{{Specifications}}

Browser compatibility

{{Compat}}

See also