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.
Note: This feature is available in Web Workers
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.
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 |
---|
HTML Standard (HTML) # the-websocket-interface |
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
WebSocket |
4 |
12 |
11
7-11
Message size limited to 16 MB (see bug 711205).
|
10 |
12.1 |
5 |
≤37 |
18 |
14
7-14
Message size limited to 16 MB (see bug 711205).
|
12.1 |
4.2 |
1.0 |
WebSocket |
4 |
12 |
11
7-11
|
10 |
12.1 |
5 |
≤37 |
18 |
14
7-14
|
12.1 |
4.2 |
1.0 |
binaryType |
15 |
12 |
11 |
10 |
12.1 |
6 |
≤37 |
18 |
14 |
12.1 |
6 |
1.0 |
bufferedAmount |
4 |
12 |
7 |
10 |
12.1 |
5 |
≤37 |
18 |
7 |
12.1 |
4.2 |
1.0 |
close |
4 |
12 |
7 |
10 |
12.1 |
5 |
≤37 |
18 |
7 |
12.1 |
4.2 |
1.0 |
close_event |
4 |
12 |
7 |
10 |
12.1 |
5 |
≤37 |
18 |
7 |
12.1 |
4.2 |
1.0 |
error_event |
5 |
12 |
7 |
10 |
12.1 |
5 |
≤37 |
18 |
7 |
12.1 |
4.2 |
1.0 |
extensions |
16 |
12 |
8 |
10 |
12.1 |
6 |
≤37 |
18 |
8 |
12.1 |
6 |
1.0 |
message_event |
4 |
12 |
7 |
10 |
12.1 |
5 |
≤37 |
18 |
7 |
12.1 |
4.2 |
1.0 |
onclose |
4 |
12 |
7 |
10 |
12.1 |
5 |
≤37 |
18 |
7 |
12.1 |
4.2 |
1.0 |
onerror |
5 |
12 |
7 |
10 |
12.1 |
5 |
≤37 |
18 |
7 |
12.1 |
4.2 |
1.0 |
onmessage |
4 |
12 |
7 |
10 |
12.1 |
5 |
≤37 |
18 |
7 |
12.1 |
4.2 |
1.0 |
onopen |
4 |
12 |
7 |
10 |
12.1 |
5 |
≤37 |
18 |
7 |
12.1 |
4.2 |
1.0 |
open_event |
4 |
12 |
7 |
10 |
12.1 |
5 |
≤37 |
18 |
7 |
12.1 |
4.2 |
1.0 |
protocol |
15 |
12 |
7 |
No |
12.1 |
6 |
≤37 |
18 |
7 |
12.1 |
6 |
1.0 |
protocol_rfc_6455 |
16 |
12 |
11 |
10 |
15 |
6 |
≤37 |
18 |
14 |
14 |
6 |
1.0 |
readyState |
4 |
12 |
7 |
10 |
12.1 |
5 |
≤37 |
18 |
7 |
12.1 |
4.2 |
1.0 |
send |
4 |
12 |
18
See bug 775368.
11-18
Only parameter of type
ArrayBuffer and String supported.8-11
Only parameter of type
String supported.7-8
Only parameter of type
String supported. Returns boolean . |
10 |
12.1 |
5 |
≤37 |
18 |
18
See bug 775368.
14-18
Only parameter of type
ArrayBuffer and String supported.8-14
Only parameter of type
String supported.7-8
Only parameter of type
String supported. Returns boolean . |
12.1 |
4.2 |
1.0 |
url |
18 |
12 |
7 |
No |
12.1 |
6 |
≤37 |
18 |
7 |
12.1 |
6 |
1.0 |
worker_support |
4 |
12 |
37 |
10 |
12.1 |
5 |
≤37 |
18 |
37 |
12.1 |
5 |
1.0 |
See also
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/WebSocket