W3cubDocs

/DOM

WebSocket.constructor

The WebSocket() constructor returns a new WebSocket object.

Syntax

var ws = new WebSocket("ws://localhost:8080");

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.

Specifications

Browser compatibilityUpdate compatibility data on GitHub

Desktop
Chrome Edge Firefox Internet Explorer Opera Safari
Basic support Yes ? 7
7
4 — 7
Parameter protocols not supported.
? Yes ?
Mobile
Android webview Chrome for Android Edge Mobile Firefox for Android Opera for Android iOS Safari Samsung Internet
Basic support ? Yes ? 7 ? ? ?

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/WebSocket