Socket.IO adapter for NestJS WebSocket framework enabling real-time communication.
npx @tessl/cli install tessl/npm-nestjs--platform-socket-io@11.1.0NestJS Platform Socket.IO provides a Socket.IO adapter implementation for the NestJS WebSocket framework. It enables real-time WebSocket communication in NestJS applications by implementing the AbstractWsAdapter interface, offering seamless integration with Socket.IO's robust WebSocket protocol with fallback support for older browsers.
npm install @nestjs/platform-socket.ioimport { IoAdapter } from '@nestjs/platform-socket.io';For CommonJS:
const { IoAdapter } = require('@nestjs/platform-socket.io');import { IoAdapter } from '@nestjs/platform-socket.io';
import { NestFactory } from '@nestjs/core';
import { INestApplication } from '@nestjs/common';
// Set up Socket.IO adapter in NestJS application
const app = await NestFactory.create(AppModule);
app.useWebSocketAdapter(new IoAdapter(app));
// Create custom server configuration
const adapter = new IoAdapter(app);
const server = adapter.create(3001, {
cors: {
origin: "http://localhost:3000",
credentials: true
}
});The IoAdapter extends NestJS's AbstractWsAdapter to provide Socket.IO specific implementations:
Creates Socket.IO server instances with flexible configuration options including namespace support.
/**
* Creates a Socket.IO server instance with optional configuration
* @param port - Port number for the server
* @param options - Optional server configuration including namespace and existing server
* @returns Socket.IO server instance
*/
create(
port: number,
options?: ServerOptions & { namespace?: string; server?: any }
): Server;
/**
* Creates a raw Socket.IO server instance
* @param port - Port number for the server
* @param options - Optional server configuration
* @returns Socket.IO server instance
*/
createIOServer(port: number, options?: any): any;Binds NestJS message handlers to Socket.IO client sockets with RxJS observables for reactive message processing.
/**
* Binds message handlers to a Socket.IO client socket with RxJS observables
* @param socket - Socket.IO client socket instance
* @param handlers - Array of message handler mappings
* @param transform - Transform function for data processing with observables
*/
bindMessageHandlers(
socket: Socket,
handlers: MessageMappingProperties[],
transform: (data: any) => Observable<any>
);Processes Socket.IO payloads to extract data and acknowledgment callbacks for proper message handling.
/**
* Maps incoming Socket.IO payload to structured data with optional acknowledgment callback
* @param payload - Raw payload from Socket.IO
* @returns Structured payload with optional acknowledgment function
*/
mapPayload(payload: unknown): { data: any; ack?: Function };Handles Socket.IO server lifecycle including proper shutdown and cleanup.
/**
* Closes the Socket.IO server instance
* @param server - Socket.IO server instance to close
* @returns Promise that resolves when server is closed
*/
async close(server: Server): Promise<void>;Provides connection lifecycle management through inherited AbstractWsAdapter methods.
/**
* Binds client connection event handler
* @param server - Socket.IO server instance
* @param callback - Function to call when clients connect
*/
bindClientConnect(server: any, callback: Function): any;
/**
* Binds client disconnection event handler
* @param client - Socket.IO client socket
* @param callback - Function to call when client disconnects
*/
bindClientDisconnect(client: any, callback: Function): any;
/**
* Controls whether to force close connections
*/
forceCloseConnections: boolean;/**
* Socket.IO adapter for NestJS WebSocket framework
* Extends AbstractWsAdapter to provide Socket.IO specific functionality
*/
class IoAdapter extends AbstractWsAdapter {
constructor(appOrHttpServer?: INestApplicationContext | object);
}
/**
* Socket.IO server configuration options with NestJS extensions
* Based on Socket.IO ServerOptions with additional namespace and server properties
*/
interface ServerOptions {
namespace?: string;
server?: any;
cors?: any;
path?: string;
connectTimeout?: number;
pingTimeout?: number;
pingInterval?: number;
[key: string]: any;
}
/**
* Message handler mapping properties for NestJS WebSocket routing
*/
interface MessageMappingProperties {
message: any;
methodName: string;
callback: (...args: any[]) => Observable<any> | Promise<any>;
}
/**
* Socket.IO server instance
*/
interface Server {
of(namespace: string): Server;
httpServer?: any;
}
/**
* Socket.IO client socket instance
*/
interface Socket {
emit(event: string, data: any): void;
on(event: string, callback: Function): void;
}
/**
* NestJS application context interface for dependency injection
*/
interface INestApplicationContext {
get<TInput = any, TResult = TInput>(
typeOrToken: any,
options?: any
): TResult;
}
/**
* RxJS Observable for reactive programming
*/
interface Observable<T> {
subscribe(observer: any): any;
}