Skip to content

WebSocket

Types

binaryType

type binaryType = WebSocketsTypes.binaryType

closeEvent

type closeEvent = WebSocketsTypes.closeEvent = {
type_: EventTypes.eventType,
target: Null.t<EventTypes.eventTarget>,
currentTarget: Null.t<EventTypes.eventTarget>,
eventPhase: int,
bubbles: bool,
cancelable: bool,
defaultPrevented: bool,
composed: bool,
isTrusted: bool,
timeStamp: float,
wasClean: bool,
code: int,
reason: string,
}

Record fields

type_
EventTypes.eventType

Returns the type of event, e.g. "click", "hashchange", or "submit". Read more on MDN

target
Null.t< EventTypes.eventTarget >

Returns the object to which event is dispatched (its target). Read more on MDN

currentTarget
Null.t< EventTypes.eventTarget >

Returns the object whose event listener's callback is currently being invoked. Read more on MDN

eventPhase
int

Returns the event's phase, which is one of NONE, CAPTURING_PHASE, AT_TARGET, and BUBBLING_PHASE. Read more on MDN

bubbles
bool

Returns true or false depending on how event was initialized. True if event goes through its target's ancestors in reverse tree order, and false otherwise. Read more on MDN

cancelable
bool

Returns true or false depending on how event was initialized. Its return value does not always carry meaning, but true can indicate that part of the operation during which event was dispatched, can be canceled by invoking the preventDefault() method. Read more on MDN

defaultPrevented
bool

Returns true if preventDefault() was invoked successfully to indicate cancelation, and false otherwise. Read more on MDN

composed
bool

Returns true or false depending on how event was initialized. True if event invokes listeners past a ShadowRoot node that is the root of its target, and false otherwise. Read more on MDN

isTrusted
bool

Returns true if event was dispatched by the user agent, and false otherwise. Read more on MDN

timeStamp
float

Returns the event's timestamp as the number of milliseconds measured relative to the time origin. Read more on MDN

wasClean
bool

Returns true if the connection closed cleanly; false otherwise. Read more on MDN

code
int

Returns the WebSocket connection close code provided by the server. Read more on MDN

reason
string

Returns the WebSocket connection close reason provided by the server. Read more on MDN

messageEvent

type messageEvent<'t> = WebSocketsTypes.messageEvent<'t> = {
type_: EventTypes.eventType,
target: Null.t<EventTypes.eventTarget>,
currentTarget: Null.t<EventTypes.eventTarget>,
eventPhase: int,
bubbles: bool,
cancelable: bool,
defaultPrevented: bool,
composed: bool,
isTrusted: bool,
timeStamp: float,
data: 't,
origin: string,
lastEventId: string,
source: Null.t<WebSocketsTypes.messageEventSource>,
ports: array<ChannelMessagingTypes.messagePort>,
}

Record fields

type_
EventTypes.eventType

Returns the type of event, e.g. "click", "hashchange", or "submit". Read more on MDN

target
Null.t< EventTypes.eventTarget >

Returns the object to which event is dispatched (its target). Read more on MDN

currentTarget
Null.t< EventTypes.eventTarget >

Returns the object whose event listener's callback is currently being invoked. Read more on MDN

eventPhase
int

Returns the event's phase, which is one of NONE, CAPTURING_PHASE, AT_TARGET, and BUBBLING_PHASE. Read more on MDN

bubbles
bool

Returns true or false depending on how event was initialized. True if event goes through its target's ancestors in reverse tree order, and false otherwise. Read more on MDN

cancelable
bool

Returns true or false depending on how event was initialized. Its return value does not always carry meaning, but true can indicate that part of the operation during which event was dispatched, can be canceled by invoking the preventDefault() method. Read more on MDN

defaultPrevented
bool

Returns true if preventDefault() was invoked successfully to indicate cancelation, and false otherwise. Read more on MDN

composed
bool

Returns true or false depending on how event was initialized. True if event invokes listeners past a ShadowRoot node that is the root of its target, and false otherwise. Read more on MDN

isTrusted
bool

Returns true if event was dispatched by the user agent, and false otherwise. Read more on MDN

timeStamp
float

Returns the event's timestamp as the number of milliseconds measured relative to the time origin. Read more on MDN

data
't

Returns the data of the message. Read more on MDN

origin
string

Returns the origin of the message, for server-sent events and cross-document messaging. Read more on MDN

lastEventId
string

Returns the last event ID string, for server-sent events. Read more on MDN

source
Null.t< WebSocketsTypes.messageEventSource >

Returns the WindowProxy of the source window, for cross-document messaging, and the MessagePort being attached, in the connect event fired at SharedWorkerGlobalScope objects. Read more on MDN

ports
array< ChannelMessagingTypes.messagePort >

Returns the MessagePort array sent with the message, for cross-document messaging and channel messaging. Read more on MDN

messageEventSource

type messageEventSource = WebSocketsTypes.messageEventSource

t

type t = WebSocketsTypes.webSocket = {
url: string,
readyState: int,
bufferedAmount: int,
extensions: string,
protocol: string,
mutable binaryType: WebSocketsTypes.binaryType,
}

Record fields

url
string

Returns the WebApiURL that was used to establish the WebSocket connection. Read more on MDN

readyState
int

Returns the state of the WebSocket object's connection. It can have the values described below. Read more on MDN

bufferedAmount
int

Returns the number of bytes of application data (UTF-8 text and binary data) that have been queued using send() but not yet been transmitted to the network.

If the WebSocket connection is closed, this attribute's value will only increase with each call to the send() method. (The number does not reset to zero once the connection closes.) Read more on MDN

extensions
string

Returns the extensions selected by the server, if any. Read more on MDN

protocol
string

Returns the subprotocol selected by the server, if any. It can be used in conjunction with the array form of the constructor's second argument to perform subprotocol negotiation. Read more on MDN

binaryType
WebSocketsTypes.binaryType

Returns a string that indicates how binary data from the WebSocket object is exposed to scripts:

Can be set, to change how binary data is returned. The default is "blob". Read more on MDN

Values

addEventListener

Appends an event listener for events whose type attribute value is type. The callback argument sets the callback that will be invoked when the event is dispatched.

The options argument sets listener-specific options. For compatibility this can be a boolean, in which case the method behaves exactly as if the value was specified as options's capture.

When set to true, options's capture prevents callback from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false (or not present), callback will not be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked if event's eventPhase attribute value is AT_TARGET.

When set to true, options's passive indicates that the callback will not cancel the event by invoking preventDefault(). This is used to enable performance optimizations described in § 2.8 Observing event listeners.

When set to true, options's once indicates that the callback will only be invoked once after which the event listener will be removed.

If an AbortSignal is passed for options's signal, then the event listener will be removed when signal is aborted.

The event listener is appended to target's event listener list and is not appended if it has the same type, callback, and capture. Read more on MDN

let addEventListener: (
t,
EventTypes.eventType,
EventTypes.eventListener<'event>,
~options: EventTypes.addEventListenerOptions=?,
) => unit

Parameters

t EventTypes.eventType EventTypes.eventListener option< EventTypes.addEventListenerOptions >

Return type

unit

addEventListenerWithCapture

Appends an event listener for events whose type attribute value is type. The callback argument sets the callback that will be invoked when the event is dispatched.

The options argument sets listener-specific options. For compatibility this can be a boolean, in which case the method behaves exactly as if the value was specified as options's capture.

When set to true, options's capture prevents callback from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false (or not present), callback will not be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked if event's eventPhase attribute value is AT_TARGET.

When set to true, options's passive indicates that the callback will not cancel the event by invoking preventDefault(). This is used to enable performance optimizations described in § 2.8 Observing event listeners.

When set to true, options's once indicates that the callback will only be invoked once after which the event listener will be removed.

If an AbortSignal is passed for options's signal, then the event listener will be removed when signal is aborted.

The event listener is appended to target's event listener list and is not appended if it has the same type, callback, and capture. Read more on MDN

let addEventListenerWithCapture: (
t,
EventTypes.eventType,
EventTypes.eventListener<'event>,
) => unit

Parameters

t EventTypes.eventType EventTypes.eventListener

Return type

unit

asEventTarget

let asEventTarget: t => EventTypes.eventTarget

Parameters

t

Return type

EventTypes.eventTarget

close

Closes the WebSocket connection, optionally using code as the the WebSocket connection close code and reason as the the WebSocket connection close reason. Read more on MDN

let close: (t, ~code: int=?, ~reason: string=?) => unit

Parameters

t option< int > option< string >

Return type

unit

dispatchEvent

Dispatches a synthetic event event to target and returns true if either event's cancelable attribute value is false or its preventDefault() method was not invoked, and false otherwise. Read more on MDN

let dispatchEvent: (t, EventTypes.event) => bool

Parameters

t EventTypes.event

Return type

bool

fromURL

fromURL(~url: string, ~protocols: string=?)

Creates a new WebSocket from a URL and an optional single protocol.

let socket = WebSocket.fromURL(~url="wss://example.com/socket")

Read more on MDN

let fromURL: (~url: string, ~protocols: string=?) => t

Parameters

string option< string >

Return type

t

fromURLWithProtocols

fromURLWithProtocols(~url: string, ~protocols: array<string>)

Creates a new WebSocket from a URL and multiple protocols.

let socket =
  WebSocket.fromURLWithProtocols(
    ~url="wss://example.com/socket",
    ~protocols=["chat", "superchat"],
  )

Read more on MDN

let fromURLWithProtocols: (~url: string, ~protocols: array<string>) => t

Parameters

string array< string >

Return type

t

removeEventListener

Removes the event listener in target's event listener list with the same type, callback, and options. Read more on MDN

let removeEventListener: (
t,
EventTypes.eventType,
EventTypes.eventListener<'event>,
~options: EventTypes.eventListenerOptions=?,
) => unit

Parameters

t EventTypes.eventType EventTypes.eventListener option< EventTypes.eventListenerOptions >

Return type

unit

removeEventListenerUseCapture

Removes the event listener in target's event listener list with the same type, callback, and options. Read more on MDN

let removeEventListenerUseCapture: (
t,
EventTypes.eventType,
EventTypes.eventListener<'event>,
) => unit

Parameters

t EventTypes.eventType EventTypes.eventListener

Return type

unit

send

Transmits data using the WebSocket connection. data can be a string, a Blob, an ArrayBuffer, or an ArrayBufferView. Read more on MDN

let send: (t, DataView.t) => unit

Parameters

t Stdlib.DataView.t

Return type

unit

sendArrayBuffer

Transmits data using the WebSocket connection. data can be a string, a Blob, an ArrayBuffer, or an ArrayBufferView. Read more on MDN

let sendArrayBuffer: (t, ArrayBuffer.t) => unit

Parameters

t Stdlib.ArrayBuffer.t

Return type

unit

sendBlob

Transmits data using the WebSocket connection. data can be a string, a Blob, an ArrayBuffer, or an ArrayBufferView. Read more on MDN

let sendBlob: (t, Blob.t) => unit

Parameters

t Blob.t

Return type

unit

sendString

Transmits data using the WebSocket connection. data can be a string, a Blob, an ArrayBuffer, or an ArrayBufferView. Read more on MDN

let sendString: (t, string) => unit

Parameters

t string

Return type

unit