The WebSocket()
constructor returns a new WebSocket
object.
Syntax
var aWebSocket = new WebSocket(url [, protocols]);
Parameters
url
- 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
SECURITY_ERR
- The port to which the connection is being attempted is being blocked.
- SyntaxError
- The URL is invalid.
Specifications
Specification | Status | Comment |
HTML Living StandardThe definition of 'the WebSocket constructor' in that specification. | Living Standard |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
WebSocket() constructor
|
Chrome
Full support Yes |
Edge
Full support ≤79 |
Firefox Full support 7 Full support 7 No support 4 — 7 Notes' Parameter |
IE
? |
Opera
Full support Yes |
Safari
? |
WebView Android
Full support Yes |
Chrome Android
Full support Yes |
Firefox Android
Full support 7 |
Opera Android
? |
Safari iOS
? |
Samsung Internet Android
Full support Yes |
Legend
- Full support
- Full support
- Compatibility unknown
- Compatibility unknown
- See implementation notes.'
- See implementation notes.
WebSocket() by Mozilla Contributors is licensed under CC-BY-SA 2.5.