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.
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:DurableObjectStuborigin:stringsocketId:stringbuildRequest(rpc:RpcCall,options:RpcCallOptions & RpcClientOptions):RequestInithandleResponse(rpc:RpcCall,response:Response):Promise<RpcClientResult<RpcResult>>sendRequest(request:RequestInit):Promise<Response>
NodeWebSocketServer
Members
listenForConnections(httpServer:Server,environmentVariables:EnvironmentVariables):void
NodeWebSocketWrapper
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:3CLOSING:2CONNECTING:0OPEN:1binaryType:BinaryTypeThe
WebSocket.binaryTypeproperty controls the type of binary data being received over the WebSocket connection.bufferedAmount:numberThe
WebSocket.bufferedAmountread-only property returns the number of bytes of data that have been queued using calls tosend()but not yet transmitted to the network.extensions:stringThe
WebSocket.extensionsread-only property returns the extensions selected by the server.onclose:(this: WebSocket, ev: CloseEvent) => any | nullonerror:(this: WebSocket, ev: Event) => any | nullonmessage:(this: WebSocket, ev: MessageEvent) => any | nullonopen:(this: WebSocket, ev: Event) => any | nullprotocol:stringThe
WebSocket.protocolread-only property returns the name of the sub-protocol the server selected; this will be one of the strings specified in theprotocolsparameter when creating the WebSocket object, or the empty string if no connection is established.readyState:numberThe
WebSocket.readyStateread-only property returns the current state of the WebSocket connection.url:stringThe
WebSocket.urlread-only property returns the absolute URL of the WebSocket as resolved by the constructor.accept():voidaddEventListener(type:K,listener:(this: WebSocket, ev: WebSocketEventMap[K]) => any,options?:boolean | AddEventListenerOptions):voidaddEventListener(type:string,listener:EventListenerOrEventListenerObject,options?:boolean | AddEventListenerOptions):voidclose(code?:number,reason?:string):voidThe
WebSocket.close()method closes the alreadyCLOSED, this method does nothing.deserializeAttachment():anydispatchEvent(event:Event):booleandispatchEvent(event:CloseEvent | Event | MessageEvent<any> | ErrorEvent):booleanThe
dispatchEvent()method of the EventTarget sends an Event to the object, (synchronously) invoking the affected event listeners in the appropriate order.removeEventListener(type:K,listener:(this: WebSocket, ev: WebSocketEventMap[K]) => any,options?:boolean | EventListenerOptions):voidremoveEventListener(type:string,listener:EventListenerOrEventListenerObject,options?:boolean | EventListenerOptions):voidremoveEventListener(type:Type,handler:EventListenerOrEventListenerObject<WebSocketEventMap[Type]>,options?:any):voidsend(data:string | ArrayBufferLike | ArrayBufferView<ArrayBufferLike> | Blob):voidsend(message:string | ArrayBuffer | ArrayBufferView<ArrayBufferLike>):voidThe
WebSocket.send()method enqueues the specified data to be transmitted to the server over the WebSocket connection, increasing the value ofbufferedAmountby the number of bytes needed to contain the data.serializeAttachment(attachment:any):voidstatic wrap(ws:WebSocket):BaseWebSocket
WebSocketDelegate
A delegate that handles web socket connections for a service.
Members
namespace:stringauthorizeUpgrade(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 | undefinedgetRemoteProcedureClientForSocket(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
WebSocketInfofor 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):voidUsed 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):voidUsed 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.
WebSocketInfoKey
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:Tphantom type brand — never assigned at runtime
_scope:"web-socket-info"phantom scope brand — never assigned at runtime
name:stringstatic create(name:string):WebSocketInfoKey<T>Creates a new WebSocketInfoKey.
Throws if a
WebSocketInfoKeywith the samenamehas already been created. Thenameshould be namespaced (e.g.'account:sessionId') to avoid collisions with other modules or app code writing to the same context bag.