Skip to main content

core.iwebsocket

Home > @medplum/core > IWebSocket

IWebSocket interface

Generic interface that an implementation of WebSocket must satisfy to be used with ReconnectingWebSocket. This is a slightly modified fork of the WebSocket global type used in Node, narrowed to exactly the members ReconnectingWebSocket actually depends on.

addEventListener/removeEventListener declare a typed overload against IWebSocketEventMap (the global event interfaces, used here rather than the local WebSocketEventMap since they are generic enough to describe the events ReconnectingWebSocket attaches to). They also declare the permissive string overload that the real WebSocket types carry, which is what lets conformant implementations whose event types do not structurally match the global ones (e.g. the ws library, whose events are a minimal subset and use target: WebSocket) still satisfy this interface.

The onopen/onclose/onmessage/onerror handler properties are intentionally omitted: ReconnectingWebSocket never assigns to them (it uses addEventListener), and because they are function-valued properties their parameters are checked contravariantly, which no single precise event type can satisfy across implementations (e.g. ws's events use target: WebSocket while the DOM's use target: EventTarget | null).

binaryType is widened to string and send() accepts the full Message union; a particular implementation should narrow each of these implementation-specific types.

Signature:

export interface IWebSocket

Properties

Property

Modifiers

Type

Description

binaryType

string

bufferedAmount

readonly

number

CLOSED

readonly

number

CLOSING

readonly

number

CONNECTING

readonly

number

extensions

readonly

string

OPEN

readonly

number

protocol

readonly

string

readyState

readonly

number

url

readonly

string

Methods

Method

Description

addEventListener(type, listener, options)

addEventListener(type, listener, options)

close(code, reason)

removeEventListener(type, listener, options)

removeEventListener(type, listener, options)

send(data)