Skip to content

SocketServerProvider

reference
1 min readUpdated

Kind: Class

Source: packages/websockets/socket-server-provider.ts

Part of: Websockets

SocketServerProvider discovers the application server and its associated event streams so WebSocket gateways can attach to the correct underlying transport. It acts as an integration layer between the WebSocket module and the HTTP application host, exposing scanForSocketServer() for resolving the active server context.

Methods

MethodSignatureReturns
scanForSocketServerscanForSocketServer(options: T, port: number)ServerAndEventStreamsHost

Where it refuses work

  • SocketServerProvider stops the work with an early return when serverAndStreamsHost && options.namespace.
  • SocketServerProvider stops the work with an early return when !namespace.
  • SocketServerProvider stops the work with an early return when !isString(namespace).

Diagram

mermaid
graph LR
  A[Application Bootstrap] --> B[SocketServerProvider]
  B --> C[scanForSocketServer()]
  C --> D[ServerAndEventStreamsHost]
  D --> E[HTTP Server]
  D --> F[Event Streams]
  E --> G[WebSocket Gateway / Adapter]

Usage

ts
import { SocketServerProvider } from '@nestjs/websockets';

class WebSocketBootstrapService {
  constructor(
    private readonly socketServerProvider: SocketServerProvider,
  ) {}

  resolveServer() {
    const serverHost = this.socketServerProvider.scanForSocketServer();

    // Use the resolved host when configuring a WebSocket adapter
    // or attaching gateway infrastructure.
    return serverHost;
  }
}

AI Coding Instructions

  • Treat SocketServerProvider as framework infrastructure; obtain it through dependency injection instead of instantiating it manually.
  • Call scanForSocketServer() only after the application server has been initialized and registered in the module/container context.
  • Preserve the ServerAndEventStreamsHost return type when passing the resolved server context to WebSocket-related integrations.
  • Avoid assuming a specific HTTP implementation; the provider should remain compatible with the configured platform adapter and server host.
  • When modifying discovery behavior, ensure both the HTTP server and event-stream capabilities remain available to WebSocket consumers.

How it works

SocketServerProvider

SocketServerProvider is a class that obtains or creates WebSocket server hosts for gateway metadata and a port. It receives a SocketsContainer and an ApplicationConfig in its constructor. [packages/websockets/socket-server-provider.ts:8-12]

Relationships

  • IMPORTS → addLeadingSlash
  • IMPORTS → isString
  • IMPORTS → ApplicationConfig

Was this page helpful?

Download as PDF
SocketServerProvider — NestJS head-to-head