EventSource: message event

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

Bubbles No
Cancelable No
Interface MessageEvent
Event handler property EventSource.onmessage

Examples

In this basic example, an EventSource is created to receive events from the server; a page with the name sse.php is responsible for generating the events.

var evtSource = new EventSource('sse.php');
var eventList = document.querySelector('ul');

evtSource.addEventListener('message', (e) => {
  var newElement = document.createElement("li");

  newElement.textContent = "message: " + e.data;
  eventList.appendChild(newElement);
});

onmessage equivalent

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

  newElement.textContent = "message: " + e.data;
  eventList.appendChild(newElement);
};

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
message_event
6
79
6
No
Yes
5
≤37
18
45
12
5
1.0

See also

© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/EventSource/message_event