W3cubDocs

/DOM

WebSocket

The WebSocket object provides the API for creating and managing a WebSocket connection to a server, as well as for sending and receiving data on the connection.

To construct a WebSocket, use the WebSocket() constructor.

Constructor

WebSocket(url[, protocols])
Returns a newly created WebSocket object.

Properties

WebSocket.binaryType
The binary data type used by the connection.
WebSocket.bufferedAmount Read only
The number of bytes of queued data.
WebSocket.extensions Read only
The extensions selected by the server.
WebSocket.onclose
An event listener to be called when the connection is closed.
WebSocket.onerror
An event listener to be called when an error occurs.
WebSocket.onmessage
An event listener to be called when a message is received from the server.
WebSocket.onopen
An event listener to be called when the connection is opened.
WebSocket.protocol Read only
The sub-protocol selected by the server.
WebSocket.readyState Read only
The current state of the connection.
WebSocket.url Read only
The absolute URL of the WebSocket.

Methods

WebSocket.close([code[, reason]])
Closes the connection.
WebSocket.send(data)
Enqueues data to be transmitted.

Example

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

// Connection opened
socket.addEventListener('open', function (event) {
    socket.send('Hello Server!');
});

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

Specifications

Specification Status Comment
HTML Living Standard
The definition of 'WebSocket' in that specification.
Living Standard Initial definition

Browser compatibilityUpdate compatibility data on GitHub

Desktop
Chrome Edge Firefox Internet Explorer Opera Safari
Basic support Yes Yes 11
11
See bug 695635.
7 — 11
Prefixed
Prefixed Implemented with the vendor prefix: Moz
Message size limited to 16 MB (see bug 711205).
4 — 6
Message size limited to 16 MB (see bug 711205).
? Yes Yes
WebSocket() constructor Yes ? 7
7
4 — 7
Parameter protocols not supported.
? Yes ?
binaryType Yes ? Yes ? Yes ?
bufferedAmount Yes ? Yes ? Yes ?
close Yes ? 8
8
4 — 8
Parameters not supported, see bug 674716.
? Yes ?
extensions Yes ? 8 ? Yes ?
onclose Yes ? Yes ? Yes ?
onerror Yes ? Yes ? Yes ?
onmessage Yes ? Yes ? Yes ?
onopen Yes ? Yes ? Yes ?
protocol Yes ? Yes ? Yes ?
readyState Yes ? Yes ? Yes ?
send Yes ? 18
18
See bug 775368.
11 — 18
Only parameter of type ArrayBuffer and String supported.
8 — 11
Only parameter of type String supported.
4 — 8
Only parameter of type String supported. Returns boolean.
? Yes ?
url Yes ? Yes ? Yes ?
Mobile
Android webview Chrome for Android Edge Mobile Firefox for Android Opera for Android iOS Safari Samsung Internet
Basic support ? ? Yes 14
14
See bug 695635.
7 — 14
Prefixed
Prefixed Implemented with the vendor prefix: Moz
Message size limited to 16 MB (see bug 711205).
? Yes ?
WebSocket() constructor ? Yes ? 7 ? ? ?
binaryType ? Yes ? Yes ? ? ?
bufferedAmount ? Yes ? Yes ? ? ?
close ? Yes ? Yes ? ? ?
extensions ? Yes ? 8 ? ? ?
onclose ? Yes ? Yes ? ? ?
onerror ? Yes ? Yes ? ? ?
onmessage ? Yes ? Yes ? ? ?
onopen ? Yes ? Yes ? ? ?
protocol ? Yes ? Yes ? ? ?
readyState ? Yes ? Yes ? ? ?
send ? Yes ? Yes ? ? ?
url ? Yes ? Yes ? ? ?

See also

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/WebSocket