GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • MDN
  • HTML: Markup language
  • Elements
  • Global attributes
  • Attributes
  • See all…
  • Responsive images
  • HTML cheatsheet
  • Date & time formats
  • See all…
  • SVG
  • MathML
  • XML
  • CSS: Styling language

This view

  • Original page
  • New address

GeezerVison is showing developer.mozilla.org — WebSocket - Web APIs | MDN

WebSocket

Baseline
Widely available
*

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

* Some parts of this feature may have varying levels of support.

  • See full compatibility
  • Learn more

Note: This feature is available in Web Workers.

The WebSocket object provides the API for creating and managing a WebSocket connection to a server, as well as for sending and receiving data on the connection.

To construct a WebSocket, use the WebSocket() constructor.

Note: The WebSocket API has no way to apply backpressure, therefore when messages arrive faster than the application can process them, the application will either fill up the device's memory by buffering those messages, become unresponsive due to 100% CPU usage, or both. For an alternative that provides backpressure automatically, see WebSocketStream.

Constructor

WebSocket()

Returns a newly created WebSocket object.

Instance properties

WebSocket.binaryType

The binary data type used by the connection.

WebSocket.bufferedAmount Read only

The number of bytes of queued data.

WebSocket.extensions Read only

The extensions selected by the server.

WebSocket.protocol Read only

The sub-protocol selected by the server.

WebSocket.readyState Read only

The current state of the connection.

WebSocket.url Read only

The absolute URL of the WebSocket.

Instance methods

WebSocket.close()

Closes the connection.

WebSocket.send()

Enqueues data to be transmitted.

Events

Listen to these events using addEventListener() or by assigning an event listener to the oneventname property of this interface.

close

Fired when a connection with a WebSocket is closed. Also available via the onclose property

error

Fired when a connection with a WebSocket has been closed because of an error, such as when some data couldn't be sent. Also available via the onerror property.

message

Fired when data is received through a WebSocket. Also available via the onmessage property.

open

Fired when a connection with a WebSocket is opened. Also available via the onopen property.

Examples

js
// Create WebSocket connection.
const socket = new WebSocket("ws://localhost:8080");

// Connection opened
socket.addEventListener("open", (event) => {
  socket.send("Hello Server!");
});

// Listen for messages
socket.addEventListener("message", (event) => {
  console.log("Message from server ", event.data);
});

Specifications

Specification
WebSockets
# the-websocket-interface

Browser compatibility

See also

  • Writing WebSocket client applications

Help improve MDN

Learn how to contribute

This page was last modified on Sep 25, 2024 by MDN contributors.

View this page on GitHub • Report a problem with this content
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.