This page is Ready to Use

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

onmessage

Summary

An event listener to be called when a message is received from the server. Receives a MessageEvent named "message".

Property of apis/websocket/WebSocketapis/websocket/WebSocket

Syntax

Note: This property is read-only.

var result = element.onmessage;

Return Value

Returns an object of type

EventHandler

Examples

socket.onmessage = function(event) {
  var data = event.data;
  // process data as string, blob, or ArrayBuffer
};

The “onmessage” event handler can also be implemented using addEventListener().

socket.addEventListener("message", function(event) {
  var data = event.data;
  // process data as string, blob, or ArrayBuffer
});

Related specifications

W3C WebSocket Specification
W3C Candidate Recommendation

Attributions