Core
defineWebSocket
Declare a typed WebSocket endpoint.
@defjs/core · Function
Signature
function defineWebSocket< const P extends string, TInput extends AnyStruct | undefined = undefined, TIncoming extends AnyStruct = AnyStruct, TOutgoing extends AnyStruct | undefined = undefined, >( definition: Omit<WebSocketDefinitionBase<TIncoming, TOutgoing>, "path"> & { input?: TInput; path: P; } & BuilderField<NoInfer<TInput>, WebSocketRequestShape<NoInfer<P>>>, ): WebSocketEndpoint< EndpointArgs<TInput>, WebSocketIncomingData<TIncoming>, WebSocketOutgoingData<TOutgoing> >;
Pass path, incoming (and optional outgoing) message structs, and queue limits. Call the returned operation with a client and, when declared, the input.
- definition —
path, oneincomingstruct, and the requiredmaxIncomingQueueSize(a positive safe integer). Optionaloutgoingandinputstructs, staticprotocols,maxOutgoingQueueSize(a non-negative safe integer), andoperation. A declaredinputalways requires an explicitbuilder.inputonly defines the parameters; the builder alone decides where each value goes. Only definitions withoutinputmay omitbuilder.
A WebSocket builder may return path and query only; headers and body are rejected. WebSocketDefinitionBase and BuilderField are internal helper types.
Parameters
Prop
Type
definition Omit<WebSocketDefinitionBase<TIncoming, TOutgoing>, "path"> & { input?: TInput; path: P; } & { builder?: (() => WebSocketRequestShape<P>) | undefined; }WebSocket contract (path, structs, queue limits, optional input).
Returns
An operation (client, ...input) => Promise<error-first tuple>.
Examples
import { defineWebSocket, struct } from "@defjs/core"; const chat = defineWebSocket({ maxIncomingQueueSize: 100, path: "/chat", protocols: ["chat.v1"], incoming: struct.object({ text: struct.string(), type: struct.literal("message"), }), outgoing: struct.object({ text: struct.string(), type: struct.literal("message"), }), });