WebSocket: message event
The message event fires when a message arrives from the server. It fires once per message.
Syntax
ws.addEventListener('message', (event) => { })Event object
WebSocketMessageEvent in the type definitions. Cannot be canceled. The callback parameter has the generic Event type, so cast it before reading data; see the example below.
data
The received content:
- A text message is a string.
- A binary message is a
Blob.sizeis the number of bytes; to read the bytes, useawait data.arrayBuffer().
When it fires
- While the connection is open, it fires once for each message received, continuing until the connection ends.
- Receiving a message larger than
maximumMessageSizefails. What fires in that case is not verified in the simulator. - A message that arrives before a listener is added is lost; it is not delivered again.
Examples
js
const ws = new WebSocket('wss://echo.websocket.org');
ws.addEventListener('message', async (e) => {
const data = /** @type {WebSocketMessageEvent} */ (e).data;
if (typeof data === 'string') {
console.log('Text', data);
} else {
const bytes = new Uint8Array(await data.arrayBuffer());
console.log('Binary', bytes.length, 'bytes');
}
});Effect: for a text message, logs its content; for a binary message, logs its byte count.
Differences from the web
- In browsers, binary messages are
Blobby default and can be changed toArrayBufferwithbinaryType; here they are alwaysBlob, and there is nobinaryType. - The browser's event object is a
MessageEventthat also carries fields such asoriginandlastEventId; here it carries onlydata. - There is no
onmessageproperty; onlyaddEventListenerworks.