The WebSocket() constructor returns a new WebSocket object.
The WebSocket() constructor returns a new WebSocket object.
js
new WebSocket(url) new WebSocket(url, protocols)
urlThe URL to which to connect; this should be the URL to which the WebSocket server will respond.
protocols Optional
Either a single protocol string or an array of protocol strings. These strings are used to indicate sub-protocols, so that a single server can implement multiple WebSocket sub-protocols (for example, you might want one server to be able to handle different types of interactions depending on the specified protocol).
If it is omitted, an empty array is used by default, i.e. [].
SyntaxError DOMException
Thrown if:
| Specification |
|---|
| WebSockets Standard # ref-for-dom-websocket-websocket① |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
WebSocket |
5 | 12 | 117–11 | 10 | 12.1 | 5 | 4.4 | 18 | 147–14 | 12.1 | 4.2 | 1.0 |
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/WebSocket