Stone.js API
    Preparing search index...

    Node.js WebSocket adapter for Stone.js.

    Runs a ws server and bridges sockets to @stone-js/realtime: each accepted socket becomes a Connection added to the shared connection store (so broadcast reaches it). Every socket event (connect, subscribe, unsubscribe, message, per-channel event, error, disconnect) is normalized into an IncomingEvent, keyed by its lifecycle/channel key, and run through the kernel, where the light key-router from @stone-js/router routes it to the matching @On* gateway method. The connection store stays in the adapter (infrastructure); dispatch is one pattern everywhere.

    ws is an optional peer dependency, imported lazily. The core is never touched: this adapter is pure Integration, normalizing socket causes into intentions.

    Hierarchy

    Index
    • Parameters

      • blueprint: IBlueprint

        The application blueprint.

      Returns NodeWsAdapter

    blueprint: IBlueprint
    hooks: AdapterHookType<NodeWsAdapterContext, RawWsResponse>
    logger: ILogger
    middleware: AdapterMixedPipeType<NodeWsAdapterContext, RawWsResponse>[]
    resolvedErrorHandlers: Record<
        string,
        IAdapterErrorHandler<RawEventType, RawResponseType, ExecutionContextType>,
    >
    server?: WsServer
    url: URL
    • Build the raw response.

      Parameters

      • context: NodeWsAdapterContext

        The event context.

      • OptionaleventHandler: AdapterEventHandlerType<IncomingEvent, OutgoingResponse>

        The event handler to be run.

      Returns Promise<RawWsResponse>

      The raw response wrapper.

    • Create the ws server, honouring an injected factory (tests / custom transports).

      Returns Promise<WsServer>

      The server.

      When ws is not installed.

    • Normalize a socket event into an IncomingEvent and route it through the kernel.

      Parameters

      • socket: WsSocket

        The originating socket.

      • connection: Connection

        The connection.

      • key: string

        The routing key (a lifecycle key or event:<channel>:<event>).

      • Optionalpayload: unknown

        The payload carried to the handler.

      Returns Promise<void>

    • Execute the event handler lifecycle hooks.

      Parameters

      • hook: KernelHookName

        The hook to execute.

      • eventHandler: AdapterEventHandlerType<IncomingEvent, OutgoingResponse>

        The event handler to be run.

      Returns Promise<void>

    • Execute adapter lifecycle hooks.

      Parameters

      • name: AdapterHookName

        The hook's name.

      • Optionalcontext: NodeWsAdapterContext

        The event context.

      • Optionalerror: any

        The error to handle.

      Returns Promise<void>

    • Handle a closed socket: drop the connection and notify gateways.

      Parameters

      • socket: WsSocket

        The originating socket.

      • connection: Connection

        The connection.

      Returns Promise<void>

    • Handle a newly accepted socket: register the connection and wire its events.

      Parameters

      Returns void

    • Handle error.

      Parameters

      Returns Promise<AdapterEventBuilderType<RawWsResponse>>

      The raw response.

    • Handle the event.

      Parameters

      • context: NodeWsAdapterContext

        The event context.

      • eventHandler: AdapterEventHandlerType<IncomingEvent, OutgoingResponse>

        The event handler to be run.

      Returns Promise<
          IAdapterEventBuilder<
              RawResponseOptions,
              IRawResponseWrapper<RawWsResponse>,
          >,
      >

      The raw response wrapper.

    • Handle an inbound frame: control frames update presence; every frame is routed through the kernel.

      Parameters

      • socket: WsSocket

        The originating socket.

      • connection: Connection

        The connection.

      • data: unknown

        The raw payload.

      Returns Promise<void>

    • Create pipeline options for the Adapter.

      Returns PipelineOptions<NodeWsAdapterContext, AdapterEventBuilderType<RawWsResponse>>

      The pipeline options for transforming the event.

    • Validate the runtime context and run the start hooks.

      Returns Promise<void>

      When used outside a Node.js context.

    • Get the event handler for the adapter.

      Returns AdapterEventHandlerType<IncomingEvent, OutgoingResponse>

      The event handler for the adapter.

      If the event handler is missing.

    • Resolve the listen port from the configured URL (defaults to 8080).

      Returns number

      The port.

    • Send the raw event through the destination.

      Parameters

      • context: NodeWsAdapterContext

        The event context.

      • eventHandler: AdapterEventHandlerType<IncomingEvent, OutgoingResponse>

        The event handler to be run.

      Returns Promise<RawWsResponse>

      Platform-specific response.

      IntegrationError

    • Send a kernel response back to the originating socket when it carries content.

      Parameters

      Returns void

    • Stop the server and run the stop hooks.

      A connected client keeps the underlying server open, and a WebSocket client stays connected by definition: close() alone therefore never calls back for as long as anyone is listening, which is a realtime server's normal state. Every client is asked to leave with 1001 Going away, the code a browser reads as "reconnect later", and whoever has not gone by the end of the grace period is dropped. A stop that cannot finish is not a stop.

      Returns Promise<void>

    • The shared connection store the default broadcaster reads, if realtime is enabled.

      Returns ConnectionStore

      The store, or undefined.

    • Validate the context and event handler.

      Parameters

      • context: NodeWsAdapterContext

        The context to validate.

      • eventHandler: AdapterEventHandlerType<IncomingEvent, OutgoingResponse>

        The event handler to validate.

      Returns void

      IntegrationError

    • Create a NodeWsAdapter.

      Parameters

      • blueprint: IBlueprint

        The application blueprint.

      Returns NodeWsAdapter

      A new instance.