Websocket API
chevron down
 

Websocket API

Interface: WebSocketEventMap

A map for the events used in WebSocket.

const wsUri = "wss://echo.websocket.org/";
const websocket = new WebSocket(wsUri);

websocket.addEventListener("open", onOpen);
websocket.addEventListener("close", onClose);
websocket.addEventListener("message", onMessage);
websocket.addEventListener("error", onError);

function onOpen(evt) {
   console.log("CONNECTED");
   websocket.send(message);
}

function onClose(evt) {
   console.log("DISCONNECTED");
}

function onMessage(evt) {
   console.log(`MESSAGE: ${evt.data}`);
}

function onError(evt) {
   console.error(`ERROR: ${evt.data}`);
}

Properties

close

CloseEvent

error

Event

message

MessageEvent

open

Event

Class: MessageEvent

The event sent by the WebSocket object when a message is received from the server.

Properties

readonly data

any

The data sent by the message emitter.

readonly defaultPrevented

boolean

Set to true when the default handling was prevented

readonly target

EventTarget or undefined

Target of the event

readonly type

string

Type of the event

Methods

new()

new(type: string, eventInitDict?: MessageEventInit | undefined)

Returns: MessageEvent

Creates a new MessageEvent object.

Interface: MessageEventInit

A dictionary used to initialize a MessageEvent object.

Properties

data

any

The data you want contained in the MessageEvent. This can be of any data type, and will default to null if not specified.

Interface: CloseEvent

The event sent by the WebSocket object when the connection closes.

Properties

readonly code

number

readonly defaultPrevented

boolean

Set to true when the default handling was prevented

readonly reason

string

readonly target

EventTarget or undefined

Target of the event

readonly type

string

Type of the event

readonly wasClean

boolean

Enum: ReadyState

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

Members

CLOSED

The connection is closed or couldn't be opened.

CLOSING

The connection is in the process of closing.

CONNECTING

The connection is not yet open.

OPEN

The connection is open and ready to communicate.

BinaryType

String: "blob" | "arraybuffer"

Type of binary data being transmitted by the connection. This should be either "blob" if DOM Blob objects are being used or "arraybuffer" if ArrayBuffer objects are being used.

Class: WebSocket

WebSocket defines an API establishing "socket" connections between a companion and a server. This implementation is following the standard API introduced in HTML5: WebSocket API. WebSocket API Documentation by Mozilla Contributors is licensed under CC-BY-SA 2.5.

Properties

readonly CLOSED

ReadyState.CLOSED

readonly CLOSING

ReadyState.CLOSING

readonly CONNECTING

ReadyState.CONNECTING

readonly OPEN

ReadyState.OPEN

binaryType

BinaryType

Type of binary data being transmitted by the connection.

readonly bufferedAmount

number

The number of bytes of data that have been queued using calls to send() but not yet transmitted to the network. This value resets to zero once all queued data has been sent. This value does not reset to zero when the connection is closed; if you keep calling send(), this will continue to climb.

onclose

((this: WebSocket, event: CloseEvent) => any) or undefined

An event listener to be called when the WebSocket connection's readyState changes to CLOSED. The listener receives a CloseEvent named "close".

onerror

((this: WebSocket, event: Event) => any) or undefined

An event listener to be called when an error occurs. This is a simple event named "error".

onmessage

((this: WebSocket, event: MessageEvent) => any) or undefined

An event listener to be called when a message is received from the server. The listener receives a MessageEvent named "message".

onopen

((this: WebSocket, event: Event) => any) or undefined

An event listener to be called when the WebSocket connection's readyState changes to OPEN; this indicates that the connection is ready to send and receive data. The event is a simple one with the name "open".

readonly readyState

ReadyState

The current state of the connection; this is one of the Ready state constants.

readonly url

string

The URL as resolved by the constructor. This is always an absolute URL.

Methods

close()

close(code?: number | undefined)

Returns: void

Closes the WebSocket connection or connection attempt, if any. If the connection is already CLOSED, this method does nothing.

send()

send(data: string | ArrayBuffer | ArrayBufferView | Blob)

Returns: void

Enqueue the specified data to be transmitted to the server over the WebSocket connection, increasing the value of bufferedAmount by the number of bytes needed to contain the data. If the data can't be sent (for example, because it needs to be buffered but the buffer is full), the socket is closed automatically.