WebSocket: message event

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Note: This feature is available in Web Workers.

The message event is fired when data is received through a WebSocket.

Syntax

Use the event name in methods like addEventListener(), or set an event handler property.

js
addEventListener("message", (event) => { })

onmessage = (event) => { }

Event type

A MessageEvent. Inherits from Event.

Event MessageEvent

Event properties

In addition to the properties listed below, properties from the parent interface, Event, are available.

data Read only

The data sent by the message emitter. The type of this property depends on the type of the WebSocket message and the value of WebSocket.binaryType.

  • If the message type is "text", then this field is a string.
  • If the message type is "binary" type, then the type of this property can be inferred from the binaryType of this socket:
    • ArrayBuffer if binaryType is "arraybuffer",
    • Blob if binaryType is "blob". This does not have an associated media type (Blob.type is "").
origin Read only

A string representing the origin of the message emitter.

Other properties from the MessageEvent interface are present, but do not pertain to the WebSocket API and are left at their default values:

Examples

js
// Create WebSocket connection.
const socket = new WebSocket("ws://localhost:8080");

// Listen for messages
socket.addEventListener("message", (event) => {
  console.log("Message from server ", event.data);
});

Specifications

Specification
WebSockets
# dom-websocket-onmessage

Browser compatibility

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Bun
Deno
Node.js
message event
Chrome – Full support
Chrome 5 (Release date: 2010-05-25)
footnote Full support
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 7 (Release date: 2011-09-27)
footnote Full support
Opera – Full support
Opera 12.1 (Release date: 2012-11-20)
footnote Full support
Safari – Full support
Safari 5 (Release date: 2010-06-07)
footnote Full support
Chrome Android – Full support
Chrome Android 18 (Release date: 2012-06-27)
footnote Full support
Firefox for Android – Full support
Firefox for Android 7 (Release date: 2011-09-27)
footnote Full support
Opera Android – Full support
Opera Android 12.1 (Release date: 2012-10-09)
footnote Full support
Safari on iOS – Full support
Safari on iOS 4.2 (Release date: 2010-11-22)
footnote Full support
Samsung Internet – Full support
Samsung Internet 1 (Release date: 2013-04-27)
footnote Full support
WebView Android – Full support
WebView Android 4.4 (Release date: 2013-12-09)
footnote Full support
WebView on iOS – Full support
WebView on iOS 4.2 (Release date: 2010-11-22)
footnote Full support
Bun – Full support
Bun 1 (Release date: 2023-09-08)
footnote Full support
Deno – Full support
Deno 1.4 (Release date: 2020-09-13)
footnote Full support
Node.js – Full support
Node.js 22 (Release date: 2024-04-24)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

See also