The WebSocket() constructor returns a new WebSocket object.


var aWebSocket = new WebSocket(url [, protocols]);


The 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 you don't specify a protocol string, an empty string is assumed.

Exceptions thrown

The port to which the connection is being attempted is being blocked.
The URL is invalid.


Specification Status Comment
HTML Living Standard
The definition of 'the WebSocket constructor' in that specification.
Living Standard

Browser compatibility

ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
WebSocket() constructorChrome Full support YesEdge Full support ≤79Firefox Full support 7
Full support 7
No support 4 — 7
Notes Parameter protocols not supported.
IE ? Opera Full support YesSafari ? WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 7Opera Android ? Safari iOS ? Samsung Internet Android Full support Yes


Full support
Full support
Compatibility unknown
Compatibility unknown
See implementation notes.
See implementation notes.