Get to know MDN better
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
* Some parts of this feature may have varying levels of support.
Note: This feature is available in Web Workers.
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.
Note: The WebSocket API has no way to apply backpressure, therefore when messages arrive faster than the application can process them, the application will either fill up the device's memory by buffering those messages, become unresponsive due to 100% CPU usage, or both. For an alternative that provides backpressure automatically, see WebSocketStream.
Returns a newly created WebSocket object.
The binary data type used by the connection.
WebSocket.bufferedAmount Read onlyThe number of bytes of queued data.
WebSocket.extensions Read onlyThe extensions selected by the server.
WebSocket.protocol Read onlyThe sub-protocol selected by the server.
WebSocket.readyState Read onlyThe current state of the connection.
WebSocket.url Read onlyThe absolute URL of the WebSocket.
Closes the connection.
WebSocket.send()Enqueues data to be transmitted.
Listen to these events using addEventListener() or by assigning an event listener to the oneventname property of this interface.
closeFired when a connection with a WebSocket is closed. Also available via the onclose property
errorFired when a connection with a WebSocket has been closed because of an error, such as when some data couldn't be sent. Also available via the onerror property.
messageFired when data is received through a WebSocket. Also available via the onmessage property.
openFired when a connection with a WebSocket is opened. Also available via the onopen property.
| WebSockets # the-websocket-interface |
Enable JavaScript to view this browser compatibility table.
This page was last modified on Sep 25, 2024 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.