WebSockets
A CloseEvent is sent to clients using WebSockets when the connection is closed. This is delivered to the listener indicated by the WebSocket object's onclose attribute.
Event CloseEvent
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 15 | 12 | 8 | 6 | 18 | 6 | |
| The CloseEvent() constructor creates a new CloseEvent object. | 16 | 14 | 11 | 6 | 18 | 6 |
| The code read-only property of the CloseEvent interface returns a WebSocket connection close code indicating the reason the connection was closed. | 15 | 12 | 8 | 6 | 18 | 6 |
| The reason read-only property of the CloseEvent interface returns the WebSocket connection close reason the server gave for closing the connection; that is, a concise human-readable prose explanation for the closure. | 15 | 12 | 8 | 6 | 18 | 6 |
| The wasClean read-only property of the CloseEvent interface returns true if the connection closed cleanly. | 15 | 12 | 8 | 6 | 18 | 6 |
| 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. | 5 | 12 | 11 | 5 | 18 | 4.2 |
| The WebSocket.binaryType property controls the type of binary data being received over the WebSocket connection. | 15 | 12 | 11 | 6 | 18 | 6 |
| The WebSocket.bufferedAmount read-only property returns the number of bytes of data that have been queued using calls to send() but not yet transmitted to the network. This value resets to zero once all queued data has been sent. This value does not reset to zero when the connection is closed; if you keep calling send(), this will continue to climb. | 5 | 12 | 7 | 5 | 18 | 4.2 |
| The WebSocket.close() method closes the WebSocket connection or connection attempt, if any. If the connection is already CLOSED, this method does nothing. | 5 | 12 | 7 | 5 | 18 | 4.2 |
| The close event is fired when a connection with a WebSocket is closed. | 5 | 12 | 7 | 5 | 18 | 4.2 |
| The error event is fired when a connection with a WebSocket has been closed due to an error (some data couldn't be sent for example). | 5 | 12 | 7 | 5 | 18 | 4.2 |
| The WebSocket.extensions read-only property returns the extensions selected by the server. This is currently only the empty string or a list of extensions as negotiated by the connection. | 16 | 12 | 8 | 6 | 18 | 6 |
| The message event is fired when data is received through a WebSocket. | 5 | 12 | 7 | 5 | 18 | 4.2 |
| The open event is fired when a connection with a WebSocket is opened. | 5 | 12 | 7 | 5 | 18 | 4.2 |
| The WebSocket.protocol read-only property returns the name of the sub-protocol the server selected; this will be one of the strings specified in the protocols parameter when creating the WebSocket object, or the empty string if no connection is established. | 15 | 12 | 7 | 6 | 18 | 6 |
protocol_rfc_6455 Supports protocol as specified by RFC 6455 | 16 | 12 | 11 | 6 | 18 | 6 |
| The WebSocket.readyState read-only property returns the current state of the WebSocket connection. | 5 | 12 | 7 | 5 | 18 | 4.2 |
| The WebSocket.send() method enqueues the specified data to be transmitted to the server over the WebSocket connection, increasing the value of bufferedAmount by the number of bytes needed to contain the data. If the data can't be sent (for example, because it needs to be buffered but the buffer is full), the socket is closed automatically. The browser will… | 5 | 12 | 18 | 5 | 18 | 4.2 |
| The WebSocket.url read-only property returns the absolute URL of the WebSocket as resolved by the constructor. | 18 | 12 | 7 | 6 | 18 | 6 |
| The WebSocket() constructor returns a new WebSocket object and immediately attempts to establish a connection to the specified WebSocket URL. | 5 | 12 | 11 | 5 | 18 | 4.2 |
WebSocket (url parameter http https relative) `url` parameter allows `https`, `http`, and relative URLs | 125 | 125 | 124 | 17.3 | 125 | 17.3 |
worker_support Available in workers | 5 | 12 | 37 | 5 | 18 | 5 |
WebSocketError Experimental | 124 | 124 | | | 124 | |
closeCode Experimental | 124 | 124 | | | 124 | |
reason Experimental | 124 | 124 | | | 124 | |
WebSocketError Experimental `WebSocketError()` constructor | 124 | 124 | | | 124 | |
| Other | ||||||
| The HTTP Sec-WebSocket-Accept response header is used in the WebSocket opening handshake to indicate that the server is willing to upgrade to a WebSocket connection. | 16 | 12 | 11 | 6 | 18 | 6 |
| The HTTP Sec-WebSocket-Extensions request header and response header is used in the WebSocket opening handshake to negotiate a protocol extension used by the client and server. | 16 | 12 | 11 | 6 | 18 | 6 |
| The HTTP Sec-WebSocket-Key request header is used in the WebSocket opening handshake to allow a client (user agent) to confirm that it "really wants" to request that an HTTP client is upgraded to become a WebSocket. | 16 | 12 | 11 | 6 | 18 | 6 |
| The HTTP Sec-WebSocket-Protocol request header and response header is used in the WebSocket opening handshake to negotiate a sub-protocol to use in the communication. This can be a well understood protocol, such as SOAP or WAMP, or a custom protocol understood by the client and server. | 16 | 12 | 11 | 6 | 18 | 6 |
| The HTTP Sec-WebSocket-Version request header and response header is used in the WebSocket opening handshake to indicate the WebSocket protocol supported by the client, and the protocol versions supported by the server if it does not support the version specified in the request. | 16 | 12 | 11 | 6 | 18 | 6 |
- Before Firefox 12, the close code `CLOSE_NORMAL` was used when the channel was closed due to an unexpected error or unspecified error condition.
- Before Firefox 8, the `WebSocket` close event was sent to the listener as a simple event.
- This feature was removed in a later browser version (11)
- Available with a vendor prefix: Moz (7)
- Message size limited to 16 MB (see bug 711205).
- This feature was removed in a later browser version (18)
- This feature was removed in a later browser version (11)
- This feature was removed in a later browser version (8)
- Only parameter of type `ArrayBuffer` and `String` supported.
- Only parameter of type `String` supported.
- Only parameter of type `String` supported. Returns `boolean`.
- This feature was removed in a later browser version (11)
- Available with a vendor prefix: Moz (7)
Use cases
-
Using WebSockets
A CloseEvent is sent to clients using WebSockets when the connection is closed.
Cautions
- May not be supported in older browsers.
Implementation notes
- Some APIs require a secure context (HTTPS) or user activation. Check the requirements before use.