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
| Method | Signature | Returns |
|---|---|---|
create | create(port: number, options: ServerOptions & { namespace?: string; server?: any }) | Server |
createIOServer | createIOServer(port: number, options: any) | any |
bindMessageHandlers | bindMessageHandlers(socket: Socket, handlers: MessageMappingProperties[], transform: (data: any) => Observable<any>) | void |
mapPayload | mapPayload(payload: unknown) | { data: any; ack?: Function } |
close | close(server: Server) | Promise<void> |
Where it refuses work
IoAdapterstops the work with an early return when!options.IoAdapterstops the work with an early return whenthis.httpServer && port === 0.IoAdapterstops the work with an early return whenresponse.event.IoAdapterstops the work with an early return whenisFunction(payload).IoAdapterstops the work with an early return whenthis.forceCloseConnections && server.httpServer === this.httpServer.
Diagram
mermaidgraph 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
tsimport { 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
IoAdapterthroughapp.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)
RedisIoAdapter—sample/02-gateways/src/adapters/redis-io.adapter.ts:6
Was this page helpful?