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 it is omitted, an empty array is used by default, i.e.
[]
.
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 Standard The definition of 'the WebSocket constructor' in that specification. |
Living Standard |
Browser compatibility
BCD tables only load in the browser