# IoAdapter

**Kind:** Class

**Source:** [`packages/platform-socket.io/adapters/io-adapter.ts`](https://github.com/nestjs/nest/blob/master/packages/platform-socket.io/adapters/io-adapter.ts#L14)

**Part of:** [Platform Socket.io](subsystem-packages-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

- `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)

- `RedisIoAdapter` — `sample/02-gateways/src/adapters/redis-io.adapter.ts`:6
