You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
The**`code`** read-only property of the {{domxref("CloseEvent")}} interface returns a[WebSocket connection close code](https://www.rfc-editor.org/rfc/rfc6455.html#section-7.1.5) indicating the reason theserver gave for closing the connection.
11
+
The**`code`** read-only property of the {{domxref("CloseEvent")}} interface returns a[WebSocket connection close code](https://www.rfc-editor.org/rfc/rfc6455.html#section-7.1.5) indicating the reason theconnection was closed.
12
12
13
13
##Value
14
14
15
-
An integer[WebSocket connection close code](https://www.rfc-editor.org/rfc/rfc6455.html#section-7.1.5) in the range`1000` -`4999`, indicating the reason theserver gave for closing the connection.
15
+
An integer[WebSocket connection close code](https://www.rfc-editor.org/rfc/rfc6455.html#section-7.1.5) in the range`1000` -`4999`, indicating the reason theconnection was closed.
Copy file name to clipboardExpand all lines: files/en-us/web/api/websocket/index.md
+3Lines changed: 3 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,6 +11,9 @@ The `WebSocket` object provides the API for creating and managing a [WebSocket](
11
11
12
12
To construct a`WebSocket`, use the[`WebSocket()`](/en-US/docs/Web/API/WebSocket/WebSocket) constructor.
13
13
14
+
>[!NOTE]
15
+
>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")}}.
Copy file name to clipboardExpand all lines: files/en-us/web/api/websockets_api/index.md
+17-8Lines changed: 17 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,12 +2,21 @@
2
2
title:The WebSocket API (WebSockets)
3
3
slug:Web/API/WebSockets_API
4
4
page-type:web-api-overview
5
-
browser-compat:api.WebSocket
5
+
browser-compat:
6
+
-api.WebSocket
7
+
-api.WebSocketStream
6
8
---
7
9
8
10
{{DefaultAPISidebar("WebSockets API")}}
9
11
10
-
The**WebSocket API** is an advanced technology that makes it possible to open a two-way interactive communication session between the user's browser and a server. With this API, you can send messages to a server and receive event-driven responses without having to poll the server for a reply.
12
+
The**WebSocket API** makes it possible to open a two-way interactive communication session between the user's browser and a server. With this API, you can send messages to a server and receive responses without having to poll the server for a reply.
13
+
14
+
The WebSocket API provides two alternative mechanisms for creating and using web socket connections: the {{domxref("WebSocket")}} interface and the {{domxref("WebSocketStream")}} interface.
15
+
16
+
- The`WebSocket` interface is stable and has good browser and server support. However it doesn't support[backpressure](/en-US/docs/Web/API/Streams_API/Concepts#backpressure). As a result, when messages arrive faster than the application can process them it will either fill up the device's memory by buffering those messages, become unresponsive due to 100% CPU usage, or both.
17
+
- The`WebSocketStream` is a {{jsxref("Promise")}}-based alternative to`WebSocket`. It uses the[Streams API](/en-US/docs/Web/API/Streams_API) to handle receiving and sending messages, meaning that socket connections can take advantage of stream backpressure automatically, regulating the speed of reading or writing to avoid bottlenecks in the application. However,`WebSocketSteam` is non-standard and currently only supported in one rendering engine.
18
+
19
+
Additionally, the[WebTransport API](/en-US/docs/Web/API/WebTransport_API) is expected to replace the WebSocket API for many applications. WebTransport is a versatile, low-level API that provides backpressure and many other features not supported by either`WebSocket` or`WebSocketStream`, such as unidirectional streams, out-of-order delivery, and unreliable data transmission via datagrams. WebTransport is more complex to use than WebSockets and its cross-browser support is not as wide, but it enables the implementation of sophisticated solutions. If standard WebSocket connections are a good fit for your use case and you need wide browser compatibility, you should employ the WebSockets API to get up and running quickly. However, if your application requires a non-standard custom solution, then you should use the WebTransport API.
11
20
12
21
>[!NOTE]
13
22
>While a WebSocket connection is functionally somewhat similar to standard Unix-style sockets, they are not related.
@@ -16,6 +25,8 @@ The **WebSocket API** is an advanced technology that makes it possible to open a
16
25
17
26
-[`WebSocket`](/en-US/docs/Web/API/WebSocket)
18
27
- : The primary interface for connecting to a WebSocket server and then sending and receiving data on the connection.
- : Promise-based interface for connecting to a WebSocket server; uses[streams](/en-US/docs/Web/API/Streams_API) to send and receive data on the connection.
19
30
-[`CloseEvent`](/en-US/docs/Web/API/CloseEvent)
20
31
- : The event sent by the WebSocket object when the connection closes.
@@ -49,28 +60,26 @@ The HTTP headers are used in the [WebSocket handshake](/en-US/docs/Web/API/WebSo
49
60
-[Writing a WebSocket server in C#](/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_server)
50
61
-[Writing a WebSocket server in Java](/en-US/docs/Web/API/WebSockets_API/Writing_a_WebSocket_server_in_Java)
51
62
-[Writing a WebSocket server in JavaScript (Deno)](/en-US/docs/Web/API/WebSockets_API/Writing_a_WebSocket_server_in_JavaScript_Deno)
63
+
-[Using WebSocketStream to write a client](/en-US/docs/Web/API/WebSockets_API/Using_WebSocketStream)
52
64
53
65
##Tools
54
66
55
67
-[AsyncAPI](https://www.asyncapi.com/): A specification for describing event-driven architectures based on protocols like WebSocket. You can use it to describe WebSocket-based APIs just as you would describe REST APIs with the OpenAPI specification. Learn[why you should consider using AsyncAPI with WebSocket](https://www.asyncapi.com/blog/websocket-part1) and[how to do so](https://www.asyncapi.com/blog/websocket-part2).
56
-
-[HumbleNet](https://hacks.mozilla.org/2017/06/introducing-humblenet-a-cross-platform-networking-library-that-works-in-the-browser/): A cross-platform networking library that works in the browser. It consists of a C wrapper around WebSockets and WebRTC that abstracts away cross-browser differences, facilitating the creation of multi-user networking functionality for games and other apps.
57
68
-[µWebSockets](https://github.com/uNetworking/uWebSockets): Highly scalable WebSocket server and client implementation for[C++11](https://isocpp.org/) and[Node.js](https://nodejs.org/).
58
69
-[Socket.IO](https://socket.io/): A long polling/WebSocket based third party transfer protocol for[Node.js](https://nodejs.org/).
59
70
-[SocketCluster](https://socketcluster.io/): A pub/sub WebSocket framework for[Node.js](https://nodejs.org/) with a focus on scalability.
60
71
-[WebSocket-Node](https://github.com/theturtle32/WebSocket-Node): A WebSocket server API implementation for[Node.js](https://nodejs.org/).
61
72
-[Total.js](https://www.totaljs.com/): Web application framework for[Node.js](https://nodejs.org/en) (Example:[WebSocket chat](https://github.com/totaljs/examples/tree/master/websocket))
62
-
-[Faye](https://www.npmjs.com/package/faye-websocket): A {{DOMxRef("WebSocket")}} (two-ways connections) and[EventSource](/en-US/docs/Web/API/EventSource) (one-way connections) for[Node.js](https://nodejs.org/) Server and Client.
63
73
-[SignalR](https://dotnet.microsoft.com/en-us/apps/aspnet/signalr): SignalR will use WebSockets under the covers when it's available, and gracefully fallback to other techniques and technologies when it isn't, while your application code stays the same.
64
74
-[Caddy](https://caddyserver.com/): A web server capable of proxying arbitrary commands (stdin/stdout) as a websocket.
65
75
-[ws](https://github.com/websockets/ws): a popular WebSocket client & server library for[Node.js](https://nodejs.org/en).
66
-
-[jsonrpc-bidirectional](https://github.com/bigstepinc/jsonrpc-bidirectional): Asynchronous RPC which, on a single connection, may have functions exported on the server and, and the same time, on the client (client may call server, server may also call client).
67
76
-[cowboy](https://github.com/ninenines/cowboy): Cowboy is a small, fast and modern HTTP server for Erlang/OTP with WebSocket support.
68
77
-[ZeroMQ](https://zeromq.org/): ZeroMQ is embeddable networking library that carries messages across in-process, IPC, TCP, UDP, TIPC, multicast and WebSocket.
69
78
-[WebSocket King](https://websocketking.com/): A client tool to help develop, test and work with WebSocket servers.
70
79
-[PHP WebSocket Server](https://github.com/napengam/phpWebSocketServer): Server written in PHP to handle connections via websockets`wss://` or`ws://` and normal sockets over`ssl://`,`tcp://`
71
-
-[Channels](https://channels.readthedocs.io/en/stable/index.html): Django library that adds support for WebSockets (and other protocols that require long running asynchronous connections).
72
-
-[Channels](https://hexdocs.pm/phoenix/channels.html): Scalable real-time communication using WebSocket in Elixir Phoenix framework.
73
-
-[LiveView](https://github.com/phoenixframework/phoenix_live_view): Real-time interactive web experiences through WebSocket in Elixir Phoenix framework.
80
+
-[DjangoChannels](https://channels.readthedocs.io/en/stable/index.html): Django library that adds support for WebSockets (and other protocols that require long running asynchronous connections).
81
+
-[(Phoenix)Channels](https://hexdocs.pm/phoenix/channels.html): Scalable real-time communication using WebSocket in Elixir Phoenix framework.
82
+
-[PhoenixLiveView](https://github.com/phoenixframework/phoenix_live_view): Real-time interactive web experiences through WebSocket in Elixir Phoenix framework.
74
83
-[Flask-SocketIO](https://flask-socketio.readthedocs.io/en/latest/): gives Flask applications access to low latency bi-directional communications between the clients and the server.
75
84
-[Gorilla WebSocket](https://pkg.go.dev/github.com/gorilla/websocket): Gorilla WebSocket is a[Go](https://go.dev/) implementation of the WebSocket protocol.