Skip to content

SocketModule

reference
3 min readUpdated

Kind: Class

Source: packages/websockets/socket-module.ts

Part of: Websockets

SocketModule coordinates WebSocket gateway registration and connection lifecycle management. It registers available gateways, connects them to the server, and provides a unified close() method for shutting down active socket resources.

Methods

MethodSignatureReturns
registerregister(container: NestContainer, applicationConfig: ApplicationConfig, graphInspector: GraphInspector, appOptions: TAppOptions, httpServer: THttpServer)void
connectAllGatewaysconnectAllGateways(providers: Map<InjectionToken, InstanceWrapper<Injectable>>, moduleName: string)void
connectGatewayToServerconnectGatewayToServer(wrapper: InstanceWrapper<Injectable>, moduleName: string)void
closeclose()Promise<any>

Where it refuses work

  • SocketModule stops the work with an early return when !metadataKeys.includes(GATEWAY_METADATA).
  • SocketModule stops the work with an early return when !this.applicationConfig.
  • SocketModule stops the work with an early return when !adapter.

Diagram

mermaid
graph LR
  App[Application Bootstrap] --> Module[SocketModule]
  Module --> Register[register()]
  Register --> Gateways[WebSocket Gateways]
  Module --> ConnectAll[connectAllGateways()]
  ConnectAll --> ConnectGateway[connectGatewayToServer()]
  ConnectGateway --> Server[WebSocket Server]
  Module --> Close[close()]
  Close --> Gateways

Usage

ts
import { SocketModule } from './packages/websockets/socket-module';

// Create the module using the dependencies required by your application.
const socketModule = new SocketModule(/* gateway and server dependencies */);

// Register configured gateways and establish their server connections.
socketModule.register();
socketModule.connectAllGateways();

// During application shutdown, close socket connections cleanly.
async function shutdown() {
  await socketModule.close();
}

AI Coding Instructions

  • Call register() before attempting to connect gateways so gateway definitions and handlers are available.
  • Use connectAllGateways() for standard application startup; use connectGatewayToServer() when connecting an individual gateway as part of custom orchestration.
  • Always await close() during shutdown to release WebSocket connections and related resources cleanly.
  • Keep gateway-specific behavior inside gateway implementations; SocketModule should remain responsible for registration and connection lifecycle coordination.
  • Ensure server dependencies are initialized before invoking gateway connection methods.

How it works

SocketModule is the WebSocket gateway lifecycle coordinator. It scans Nest container providers for classes marked with the websockets:is_gateway metadata key, initializes a WebSocket adapter when it finds the first such gateway, and delegates gateway attachment to WebSocketsController. packages/websockets/socket-module.ts:68-95 The @WebSocketGateway() decorator writes that marker together with gateway port and options metadata. packages/websockets/decorators/socket-gateway.decorator.ts:20-29

Relationships

  • IMPORTS → NestApplicationOptions
  • IMPORTS → InjectionToken
  • IMPORTS → Injectable
  • IMPORTS → NestApplicationContextOptions
  • IMPORTS → ApplicationConfig
  • IMPORTS → GuardsConsumer
  • IMPORTS → GuardsContextCreator
  • IMPORTS → loadAdapter
  • IMPORTS → NestContainer
  • IMPORTS → InstanceWrapper
  • IMPORTS → GraphInspector
  • IMPORTS → InterceptorsConsumer
  • IMPORTS → InterceptorsContextCreator
  • IMPORTS → PipesConsumer
  • IMPORTS → PipesContextCreator
  • IMPORTS → -nestjs-platform-socket-io

Used by

2 references from 2 files. Each is a place in this repository where the symbol is actually used — go read one rather than trusting an example.

Imported by (2)

  • NestApplicationpackages/core/nest-application.ts:54
  • NestMicroservicepackages/microservices/nest-microservice.ts:35

Was this page helpful?

Download as PDF
SocketModule — NestJS head-to-head