Classes

@system-inc/base-foundation · 515c140 · 6 symbols

A service that provides a bridge between durable objects and web sockets. Runs on the durable object to allow RPCs to flow from a worker through the durable object to the web socket.

Members

  • broadcastWebSocketEvent(event: WebSocketEvent, tags?: string[]): Promise<void>

    Broadcasts a web socket event to all web sockets with the provided tags. If no tags are provided, the event is broadcast to all web sockets.

    The event originates from a worker and is sent through a durable object to the web sockets.

  • emitWebSocketEvent(event: T, socketId: string): Promise<void>

    Emits the event provided on the web socket for the id provided.

    Forwards an event originating from a worker through a durable object to the web socket with the socketId.

  • getWebSocketInfo(socketId: string): Promise<WebSocketInfo | undefined>

    Gets the web socket info for the provided socketId.

View source ↗

An RPC driver that uses a Durable Object to send RPCs to a client that is connected to it via a web socket.

extends RpcClientDriver

Members

  • durableObjectStub: DurableObjectStub

  • origin: string

  • socketId: string

  • buildRequest(rpc: RpcCall, options: RpcCallOptions & RpcClientOptions): RequestInit

  • handleResponse(rpc: RpcCall, response: Response): Promise<RpcClientResult<RpcResult>>

  • sendRequest(request: RequestInit): Promise<Response>

View source ↗

Members

View source ↗

A wrapper that takes a NodeWebSocket and wraps it in the BaseWebSocket interface so that it can be used in the rest of the Base socket systems.

implements BaseWebSocket

Members

  • CLOSED: 3

  • CLOSING: 2

  • CONNECTING: 0

  • OPEN: 1

  • binaryType: BinaryType

    The WebSocket.binaryType property controls the type of binary data being received over the WebSocket connection.

    MDN Reference

  • bufferedAmount: number

    The WebSocket.bufferedAmount read-only property returns the number of bytes of data that have been queued using calls to send() but not yet transmitted to the network.

    MDN Reference

  • extensions: string

    The WebSocket.extensions read-only property returns the extensions selected by the server.

    MDN Reference

  • onclose: (this: WebSocket, ev: CloseEvent) => any | null

  • onerror: (this: WebSocket, ev: Event) => any | null

  • onmessage: (this: WebSocket, ev: MessageEvent) => any | null

  • onopen: (this: WebSocket, ev: Event) => any | null

  • protocol: string

    The WebSocket.protocol read-only property returns the name of the sub-protocol the server selected; this will be one of the strings specified in the protocols parameter when creating the WebSocket object, or the empty string if no connection is established.

    MDN Reference

  • readyState: number

    The WebSocket.readyState read-only property returns the current state of the WebSocket connection.

    MDN Reference

  • url: string

    The WebSocket.url read-only property returns the absolute URL of the WebSocket as resolved by the constructor.

    MDN Reference

  • accept(): void

  • addEventListener(type: K, listener: (this: WebSocket, ev: WebSocketEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void

    addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void

  • close(code?: number, reason?: string): void

    The WebSocket.close() method closes the already CLOSED, this method does nothing.

    MDN Reference

  • deserializeAttachment(): any

  • dispatchEvent(event: Event): boolean

    dispatchEvent(event: CloseEvent | Event | MessageEvent<any> | ErrorEvent): boolean

    The dispatchEvent() method of the EventTarget sends an Event to the object, (synchronously) invoking the affected event listeners in the appropriate order.

    MDN Reference

  • removeEventListener(type: K, listener: (this: WebSocket, ev: WebSocketEventMap[K]) => any, options?: boolean | EventListenerOptions): void

    removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void

    removeEventListener(type: Type, handler: EventListenerOrEventListenerObject<WebSocketEventMap[Type]>, options?: any): void

  • send(data: string | ArrayBufferLike | ArrayBufferView<ArrayBufferLike> | Blob): void

    send(message: string | ArrayBuffer | ArrayBufferView<ArrayBufferLike>): void

    The WebSocket.send() method enqueues 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.

    MDN Reference

  • serializeAttachment(attachment: any): void

  • static wrap(ws: WebSocket): BaseWebSocket

View source ↗

A delegate that handles web socket connections for a service.

Members

  • namespace: string

  • authorizeUpgrade(request: RequestContext): Promise<string | undefined>

    Determines if a upgrade request (web socket connection) should be authorized (allowed).

    If it is allowed, the ID of the socket should be returned, otherwise undefined should be returned.

  • emitEventOnSocket(event: T, socketId: string): void | Promise<void>

  • getDurableObjectForSocket(socketId: string): CfDurableObjectHandle | undefined

  • getRemoteProcedureClientForSocket(socketId: string): RpcClient<RpcInterface>

  • getWebSocketInfoFromSocket(socketId: string): Promise<WebSocketInfo | undefined>

    Reads the WebSocketInfo for a connected socket. On Cloudflare this queries the Durable Object bridge (which owns the socket) via a Fetcher RPC — the same transport emitEventOnSocket uses — NOT getRemoteProcedureClientForSocket, whose driver pushes events to the connected client rather than answering DO-side queries.

  • onWebSocketClose(webSocket: BaseWebSocket, socketInfo: WebSocketInfo): void | Promise<void>

  • onWebSocketError(webSocket: BaseWebSocket, socketInfo: WebSocketInfo, error: WebSocketErrorEvent): void | Promise<void>

  • onWebSocketEvent(webSocket: BaseWebSocket, webSocketInfo: WebSocketInfo, event: WebSocketEvent): void | Promise<void>

  • populateSocketContext(info: WebSocketInfo, socketId: string): void | Promise<void>

    Optional hook for populating application-specific values on the WebSocketInfo context bag for this socket.

    Called lazily on first use of WebSocketInfo for a given socket and cached thereafter. Use setWsContext with your own WebSocketInfoKeys to add values the rest of your code will read via getWsContext.

    Framework-module values (device id, session id, etc.) are populated automatically at connect time and do not need to be handled here.

  • registerSocket(webSocket: BaseWebSocket, webSocketInfo: WebSocketInfo): void

    Used to register a socket with the service. This is used to bind the socket to the context and delegate for the connection.

    Used only in Node environments.

  • unregisterSocket(webSocketInfo: WebSocketInfo): void

    Used to unregister a socket with the delegate. After this is called, the socket will no longer be bound to the context and delegate.

    Used only in Node environments.

View source ↗

A branded, typed key used to read and write values on a WebSocketInfo's context bag.

Values must be JSON-serializable so they survive the wire transfer that carries WebSocketInfo across workers / durable objects / RPC message meta.

Modules publish their own keys (e.g. device id, session id) and applications publish their own domain-specific keys (e.g. a GridNode). All of them share the same bag.

Extends TypedKey with the 'web-socket-info' scope brand so WebSocket keys cannot be confused with request-context, environment, or module keys at the type level.

Two keys with the same name would collide on the shared context bag and silently overwrite each other. create enforces uniqueness at creation time and throws on duplicates. Namespace the name (e.g. 'account:sessionId') so cross-module keys don't collide.

extends TypedKey<T, "web-socket-info">

Members

  • _brand: T

    phantom type brand — never assigned at runtime

  • _scope: "web-socket-info"

    phantom scope brand — never assigned at runtime

  • name: string

  • static create(name: string): WebSocketInfoKey<T>

    Creates a new WebSocketInfoKey.

    Throws if a WebSocketInfoKey with the same name has already been created. The name should be namespaced (e.g. 'account:sessionId') to avoid collisions with other modules or app code writing to the same context bag.

View source ↗

Classes • Documentation • Base