Skip to content

WebSocket

Defined in: node_modules/.pnpm/@cloudflare+workers-types@4.20250725.0/node_modules/@cloudflare/workers-types/index.ts:2825

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

MDN Reference

  • EventTarget<WebSocketEventMap>

readonly extensions: null | string

Defined in: node_modules/.pnpm/@cloudflare+workers-types@4.20250725.0/node_modules/@cloudflare/workers-types/index.ts:2881

Returns the extensions selected by the server, if any.

MDN Reference


readonly protocol: null | string

Defined in: node_modules/.pnpm/@cloudflare+workers-types@4.20250725.0/node_modules/@cloudflare/workers-types/index.ts:2875

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.

MDN Reference


readonly readyState: number

Defined in: node_modules/.pnpm/@cloudflare+workers-types@4.20250725.0/node_modules/@cloudflare/workers-types/index.ts:2863

Returns the state of the WebSocket object’s connection. It can have the values described below.

MDN Reference


readonly url: null | string

Defined in: node_modules/.pnpm/@cloudflare+workers-types@4.20250725.0/node_modules/@cloudflare/workers-types/index.ts:2869

Returns the URL that was used to establish the WebSocket connection.

MDN Reference

accept(): void

Defined in: node_modules/.pnpm/@cloudflare+workers-types@4.20250725.0/node_modules/@cloudflare/workers-types/index.ts:2843

void


addEventListener<Type>(type, handler, options?): void

Defined in: node_modules/.pnpm/@cloudflare+workers-types@4.20250725.0/node_modules/@cloudflare/workers-types/index.ts:812

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.

MDN Reference

Type extends keyof WebSocketEventMap

Type

EventListenerOrEventListenerObject<WebSocketEventMap[Type]>

boolean | EventTargetAddEventListenerOptions

void

EventTarget.addEventListener


close(code?, reason?): void

Defined in: node_modules/.pnpm/@cloudflare+workers-types@4.20250725.0/node_modules/@cloudflare/workers-types/index.ts:2855

Closes the WebSocket connection, optionally using code as the the WebSocket connection close code and reason as the the WebSocket connection close reason.

MDN Reference

number

string

void


deserializeAttachment(): any

Defined in: node_modules/.pnpm/@cloudflare+workers-types@4.20250725.0/node_modules/@cloudflare/workers-types/index.ts:2857

any


dispatchEvent(event): boolean

Defined in: node_modules/.pnpm/@cloudflare+workers-types@4.20250725.0/node_modules/@cloudflare/workers-types/index.ts:832

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.

MDN Reference

Event | MessageEvent | CloseEvent | ErrorEvent

boolean

EventTarget.dispatchEvent


removeEventListener<Type>(type, handler, options?): void

Defined in: node_modules/.pnpm/@cloudflare+workers-types@4.20250725.0/node_modules/@cloudflare/workers-types/index.ts:822

Removes the event listener in target’s event listener list with the same type, callback, and options.

MDN Reference

Type extends keyof WebSocketEventMap

Type

EventListenerOrEventListenerObject<WebSocketEventMap[Type]>

boolean | EventTargetEventListenerOptions

void

EventTarget.removeEventListener


send(message): void

Defined in: node_modules/.pnpm/@cloudflare+workers-types@4.20250725.0/node_modules/@cloudflare/workers-types/index.ts:2849

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

MDN Reference

string | ArrayBuffer | ArrayBufferView<ArrayBufferLike>

void


serializeAttachment(attachment): void

Defined in: node_modules/.pnpm/@cloudflare+workers-types@4.20250725.0/node_modules/@cloudflare/workers-types/index.ts:2856

any

void