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
| Method | Signature | Returns |
|---|---|---|
scanForSocketServer | scanForSocketServer(options: T, port: number) | ServerAndEventStreamsHost |
Where it refuses work
SocketServerProviderstops the work with an early return whenserverAndStreamsHost && options.namespace.SocketServerProviderstops the work with an early return when!namespace.SocketServerProviderstops the work with an early return when!isString(namespace).
Diagram
mermaidgraph 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
tsimport { 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
SocketServerProvideras 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
ServerAndEventStreamsHostreturn 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?