This page is Ready to Use

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

readyState

Summary

The current state of the connection, represented as a numeric constant.

Property of apis/websocket/WebSocketapis/websocket/WebSocket

Syntax

Note: This property is read-only.

var result = element.readyState;

Return Value

Returns an object of type unsigned shortunsigned short

These constants are used by the readyState attribute to describe the state of the WebSocket connection.

ConstantValueDescription
CONNECTING0The connection is not yet open.
OPEN1The connection is open and ready to communicate.
CLOSING2The connection is in the process of closing.
CLOSED3The connection is closed or couldn’t be opened.

Examples

switch (socket.readyState) {
  case WebSocket.CONNECTING:
    // do something
    break;
  case WebSocket.OPEN:
    // do something
    break;
  case WebSocket.CLOSING:
    // do something
    break;
  case WebSocket.CLOSED:
    // do something
    break;
  default:
    // this never happens
    break;
}

Related specifications

W3C WebSocket Specification
W3C Candidate Recommendation

Attributions