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.
Constants
Constant | Value |
WebSocket.CONNECTING
|
0
|
WebSocket.OPEN
|
1
|
WebSocket.CLOSING
|
2
|
WebSocket.CLOSED
|
3
|
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.
Events
Listen to these events using addEventListener()
or by assigning an event listener to the oneventname
property of this interface.
close
- Fired when a connection with a
WebSocket
is closed. Also available via theonclose
property error
- Fired 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 theonerror
property. message
- Fired when data is received through a
WebSocket
. Also available via theonmessage
property. open
- Fired when a connection with a
WebSocket
is opened. Also available via theonopen
property.
Examples
// 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 |
---|---|
HTML Living StandardThe definition of 'WebSocket' in that specification. | Living Standard |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
WebSocket
|
Chrome
Full support 4 |
Edge
Full support 12 |
Firefox Full support 11 Full support 11 No support 7 — 11 Prefixed' Implemented with the vendor prefix: Moz Notes' Message size limited to 16 MB (see bug 711205). No support 4 — 6 Notes' Message size limited to 16 MB (see bug 711205). |
IE
Full support 10 |
Opera
Full support 12.1 |
Safari
Full support 5 |
WebView Android
Full support ≤37 |
Chrome Android
Full support 18 |
Firefox Android Full support 14 Full support 14 Notes' See bug 695635. No support 7 — 14 Prefixed' Implemented with the vendor prefix: Moz Notes' Message size limited to 16 MB (see bug 711205). No support 4 — 6 Notes' Message size limited to 16 MB (see bug 711205). |
Opera Android
Full support 12.1 |
Safari iOS
Full support 4.2 |
Samsung Internet Android
Full support 1.0 |
WebSocket() constructor
|
Chrome
Full support Yes |
Edge
Full support ≤79 |
Firefox Full support 7 Full support 7 No support 4 — 7 Notes' Parameter |
IE
? |
Opera
Full support Yes |
Safari
? |
WebView Android
Full support Yes |
Chrome Android
Full support Yes |
Firefox Android
Full support 7 |
Opera Android
? |
Safari iOS
? |
Samsung Internet Android
Full support Yes |
binaryType
|
Chrome
Full support Yes |
Edge
Full support 12 |
Firefox
Full support Yes |
IE
? |
Opera
Full support Yes |
Safari
Full support Yes |
WebView Android
Full support Yes |
Chrome Android
Full support Yes |
Firefox Android
Full support Yes |
Opera Android
Full support Yes |
Safari iOS
Full support Yes |
Samsung Internet Android
Full support Yes |
bufferedAmount
|
Chrome
Full support Yes |
Edge
Full support 12 |
Firefox
Full support Yes |
IE
? |
Opera
Full support Yes |
Safari
Full support Yes |
WebView Android
Full support Yes |
Chrome Android
Full support Yes |
Firefox Android
Full support Yes |
Opera Android
Full support Yes |
Safari iOS
Full support Yes |
Samsung Internet Android
Full support Yes |
close
|
Chrome
Full support 4 |
Edge
Full support 12 |
Firefox Full support 8 |
IE
Full support 10 |
Opera
Full support 12.1 |
Safari
Full support 5 |
WebView Android
Full support ≤37 |
Chrome Android
Full support 18 |
Firefox Android Full support 8 |
Opera Android
Full support 12.1 |
Safari iOS
Full support 4.2 |
Samsung Internet Android
Full support 1.0 |
close event
|
Chrome
Full support Yes |
Edge
Full support 12 |
Firefox
Full support Yes |
IE
? |
Opera
Full support Yes |
Safari
? |
WebView Android
Full support Yes |
Chrome Android
Full support Yes |
Firefox Android
Full support Yes |
Opera Android
? |
Safari iOS
? |
Samsung Internet Android
Full support Yes |
error event
|
Chrome
Full support Yes |
Edge
Full support 12 |
Firefox
Full support Yes |
IE
? |
Opera
Full support Yes |
Safari
? |
WebView Android
Full support Yes |
Chrome Android
Full support Yes |
Firefox Android
Full support Yes |
Opera Android
? |
Safari iOS
? |
Samsung Internet Android
Full support Yes |
extensions
|
Chrome
Full support Yes |
Edge
Full support 12 |
Firefox
Full support 8 |
IE
? |
Opera
Full support Yes |
Safari
Full support Yes |
WebView Android
Full support Yes |
Chrome Android
Full support Yes |
Firefox Android
Full support 8 |
Opera Android
Full support Yes |
Safari iOS
Full support Yes |
Samsung Internet Android
Full support Yes |
message event
|
Chrome
Full support Yes |
Edge
Full support 12 |
Firefox
Full support Yes |
IE
? |
Opera
Full support Yes |
Safari
? |
WebView Android
Full support Yes |
Chrome Android
Full support Yes |
Firefox Android
Full support Yes |
Opera Android
? |
Safari iOS
? |
Samsung Internet Android
Full support Yes |
onclose
|
Chrome
Full support Yes |
Edge
Full support 12 |
Firefox
Full support Yes |
IE
? |
Opera
Full support Yes |
Safari
Full support Yes |
WebView Android
Full support Yes |
Chrome Android
Full support Yes |
Firefox Android
Full support Yes |
Opera Android
Full support Yes |
Safari iOS
Full support Yes |
Samsung Internet Android
Full support Yes |
onerror
|
Chrome
Full support Yes |
Edge
Full support 12 |
Firefox
Full support Yes |
IE
? |
Opera
Full support Yes |
Safari
Full support Yes |
WebView Android
Full support Yes |
Chrome Android
Full support Yes |
Firefox Android
Full support Yes |
Opera Android
Full support Yes |
Safari iOS
Full support Yes |
Samsung Internet Android
Full support Yes |
onmessage
|
Chrome
Full support Yes |
Edge
Full support 12 |
Firefox
Full support Yes |
IE
? |
Opera
Full support Yes |
Safari
Full support Yes |
WebView Android
Full support Yes |
Chrome Android
Full support Yes |
Firefox Android
Full support Yes |
Opera Android
Full support Yes |
Safari iOS
Full support Yes |
Samsung Internet Android
Full support Yes |
onopen
|
Chrome
Full support Yes |
Edge
Full support 12 |
Firefox
Full support Yes |
IE
? |
Opera
Full support Yes |
Safari
Full support Yes |
WebView Android
Full support Yes |
Chrome Android
Full support Yes |
Firefox Android
Full support Yes |
Opera Android
Full support Yes |
Safari iOS
Full support Yes |
Samsung Internet Android
Full support Yes |
open event
|
Chrome
Full support Yes |
Edge
Full support 12 |
Firefox
Full support Yes |
IE
? |
Opera
Full support Yes |
Safari
? |
WebView Android
Full support Yes |
Chrome Android
Full support Yes |
Firefox Android
Full support Yes |
Opera Android
? |
Safari iOS
? |
Samsung Internet Android
Full support Yes |
protocol
|
Chrome
Full support Yes |
Edge
Full support 12 |
Firefox
Full support Yes |
IE
? |
Opera
Full support Yes |
Safari
Full support Yes |
WebView Android
Full support Yes |
Chrome Android
Full support Yes |
Firefox Android
Full support Yes |
Opera Android
Full support Yes |
Safari iOS
Full support Yes |
Samsung Internet Android
Full support Yes |
Supports protocol as specified by RFC 6455 | Chrome
Full support 16 |
Edge
Full support 12 |
Firefox
Full support 11 |
IE
Full support 10 |
Opera
Full support 15 |
Safari
Full support 6 |
WebView Android
Full support Yes |
Chrome Android
Full support 18 |
Firefox Android
Full support 14 |
Opera Android
Full support 14 |
Safari iOS
Full support 6 |
Samsung Internet Android
Full support 1.0 |
readyState
|
Chrome
Full support 43 |
Edge
Full support 12 |
Firefox
Full support 19 |
IE
Full support 10 |
Opera
Full support 30 |
Safari
Full support 10 |
WebView Android
Full support 43 |
Chrome Android
Full support 43 |
Firefox Android
Full support 19 |
Opera Android
Full support 30 |
Safari iOS
Full support 10 |
Samsung Internet Android
Full support 4.0 |
send
|
Chrome
Full support 4 |
Edge
Full support 12 |
Firefox Full support 18 Full support 18 Notes' See bug 775368. No support 11 — 18 Notes' Only parameter of type Notes' Only parameter of type Notes' Only parameter of type |
IE
Full support 10 |
Opera
Full support 12.1 |
Safari
Full support 5 |
WebView Android
Full support ≤37 |
Chrome Android
Full support 18 |
Firefox Android Full support 18 Full support 18 Notes' See bug 775368. No support 14 — 18 Notes' Only parameter of type Notes' Only parameter of type Notes' Only parameter of type |
Opera Android
Full support 12.1 |
Safari iOS
Full support 4.2 |
Samsung Internet Android
Full support 1.0 |
url
|
Chrome
Full support Yes |
Edge
Full support 12 |
Firefox
Full support Yes |
IE
? |
Opera
Full support Yes |
Safari
Full support Yes |
WebView Android
Full support Yes |
Chrome Android
Full support Yes |
Firefox Android
Full support Yes |
Opera Android
Full support Yes |
Safari iOS
Full support Yes |
Samsung Internet Android
Full support Yes |
Available in workers | Chrome
Full support Yes |
Edge
Full support ≤18 |
Firefox
Full support 37 |
IE
? |
Opera
? |
Safari
? |
WebView Android
Full support Yes |
Chrome Android
Full support Yes |
Firefox Android
Full support 37 |
Opera Android
? |
Safari iOS
? |
Samsung Internet Android
Full support Yes |
Legend
- Full support
- Full support
- Compatibility unknown
- Compatibility unknown
- See implementation notes.'
- See implementation notes.
- Requires a vendor prefix or different name for use.'
- Requires a vendor prefix or different name for use.
See also
WebSocket by Mozilla Contributors is licensed under CC-BY-SA 2.5.