Skip to content

IoAdapter

reference
1 min readUpdated

Kind: Class

Source: packages/platform-socket.io/adapters/io-adapter.ts

Part of: Platform Socket.io

IoAdapter is NestJS's Socket.IO transport adapter, responsible for creating and configuring Socket.IO servers for WebSocket gateways. It binds gateway message handlers to connected sockets, maps incoming payloads and acknowledgement callbacks, and closes the underlying server during application shutdown.

Extends: AbstractWsAdapter

Methods

MethodSignatureReturns
createcreate(port: number, options: ServerOptions & { namespace?: string; server?: any })Server
createIOServercreateIOServer(port: number, options: any)any
bindMessageHandlersbindMessageHandlers(socket: Socket, handlers: MessageMappingProperties[], transform: (data: any) => Observable<any>)void
mapPayloadmapPayload(payload: unknown){ data: any; ack?: Function }
closeclose(server: Server)Promise<void>

Where it refuses work

  • IoAdapter stops the work with an early return when !options.
  • IoAdapter stops the work with an early return when this.httpServer && port === 0.
  • IoAdapter stops the work with an early return when response.event.
  • IoAdapter stops the work with an early return when isFunction(payload).
  • IoAdapter stops the work with an early return when this.forceCloseConnections && server.httpServer === this.httpServer.

Diagram

mermaid
graph LR
  App[Nest Application] --> Adapter[IoAdapter]
  Adapter --> Server[Socket.IO Server]
  Server --> Gateway[WebSocket Gateway]
  Gateway --> Handlers[Message Handlers]
  Client[Socket.IO Client] -->|event + payload| Server
  Handlers -->|response / acknowledgement| Client

Usage

ts
import { NestFactory } from '@nestjs/core';
import { IoAdapter } from '@nestjs/platform-socket.io';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  // Register the Socket.IO adapter used by @WebSocketGateway() classes.
  app.useWebSocketAdapter(new IoAdapter(app));

  await app.listen(3000);
}

bootstrap();

AI Coding Instructions

  • Use IoAdapter through app.useWebSocketAdapter() when configuring Socket.IO support for NestJS gateways.
  • Prefer gateway decorators such as @WebSocketGateway() and @SubscribeMessage() instead of manually binding Socket.IO events.
  • Preserve acknowledgement callback handling when changing payload parsing; Socket.IO payloads may include a final callback function.
  • Ensure custom adapters or server instances implement proper shutdown behavior so close() can release Socket.IO resources.
  • Configure Socket.IO options, such as CORS or transport settings, when creating or extending the adapter rather than modifying gateway handler logic.

Relationships

  • IMPORTS → isFunction
  • IMPORTS → isNil
  • IMPORTS → AbstractWsAdapter
  • IMPORTS → MessageMappingProperties
  • IMPORTS → DISCONNECT_EVENT

Used by

1 reference from 1 file. Each is a place in this repository where the symbol is actually used — go read one rather than trusting an example.

Imported by (1)

  • RedisIoAdaptersample/02-gateways/src/adapters/redis-io.adapter.ts:6

Was this page helpful?

Download as PDF
IoAdapter — NestJS head-to-head