-
Notifications
You must be signed in to change notification settings - Fork 22.7k
/
Copy pathindex.md
90 lines (66 loc) · 3.12 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
---
title: WebSocket
slug: Web/API/WebSocket
page-type: web-api-interface
browser-compat: api.WebSocket
---
{{APIRef("WebSockets API")}}{{AvailableInWorkers}}
The `WebSocket` object provides the API for creating and managing a [WebSocket](/en-US/docs/Web/API/WebSockets_API) connection to a server, as well as for sending and receiving data on the connection.
To construct a `WebSocket`, use the [`WebSocket()`](/en-US/docs/Web/API/WebSocket/WebSocket) constructor.
> [!NOTE]
> The `WebSocket` API has no way to apply [backpressure](/en-US/docs/Web/API/Streams_API/Concepts#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 {{domxref("WebSocketStream")}}.
{{InheritanceDiagram}}
## Constructor
- {{domxref("WebSocket.WebSocket", "WebSocket()")}}
- : Returns a newly created `WebSocket` object.
## Instance properties
- {{domxref("WebSocket.binaryType")}}
- : The binary data type used by the connection.
- {{domxref("WebSocket.bufferedAmount")}} {{ReadOnlyInline}}
- : The number of bytes of queued data.
- {{domxref("WebSocket.extensions")}} {{ReadOnlyInline}}
- : The extensions selected by the server.
- {{domxref("WebSocket.protocol")}} {{ReadOnlyInline}}
- : The sub-protocol selected by the server.
- {{domxref("WebSocket.readyState")}} {{ReadOnlyInline}}
- : The current state of the connection.
- {{domxref("WebSocket.url")}} {{ReadOnlyInline}}
- : The absolute URL of the WebSocket.
## Instance methods
- {{domxref("WebSocket.close()")}}
- : Closes the connection.
- {{domxref("WebSocket.send()")}}
- : 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.
- {{domxref("WebSocket/close_event", "close")}}
- : Fired when a connection with a `WebSocket` is closed.
Also available via the `onclose` property
- {{domxref("WebSocket/error_event", "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 the `onerror` property.
- {{domxref("WebSocket/message_event", "message")}}
- : Fired when data is received through a `WebSocket`.
Also available via the `onmessage` property.
- {{domxref("WebSocket/open_event", "open")}}
- : Fired when a connection with a `WebSocket` is opened.
Also available via the `onopen` property.
## Examples
```js
// Create WebSocket connection.
const socket = new WebSocket("ws://localhost:8080");
// Connection opened
socket.addEventListener("open", (event) => {
socket.send("Hello Server!");
});
// Listen for messages
socket.addEventListener("message", (event) => {
console.log("Message from server ", event.data);
});
```
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- [Writing WebSocket client applications](/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications)