or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-nestjs--platform-socket-io

Socket.IO adapter for NestJS WebSocket framework enabling real-time communication.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@nestjs/platform-socket.io@11.1.x

To install, run

npx @tessl/cli install tessl/npm-nestjs--platform-socket-io@11.1.0

index.mddocs/

NestJS Platform Socket.IO

NestJS 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.

Package Information

  • Package Name: @nestjs/platform-socket.io
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install @nestjs/platform-socket.io

Core Imports

import { IoAdapter } from '@nestjs/platform-socket.io';

For CommonJS:

const { IoAdapter } = require('@nestjs/platform-socket.io');

Basic Usage

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
  }
});

Architecture

The IoAdapter extends NestJS's AbstractWsAdapter to provide Socket.IO specific implementations:

  • Server Creation: Handles Socket.IO server instantiation and configuration
  • Namespace Support: Manages Socket.IO namespaces for organizing connections
  • Message Handling: Routes messages between NestJS and Socket.IO using RxJS observables
  • Payload Processing: Transforms Socket.IO payloads and handles acknowledgment functions
  • Connection Management: Manages client connections and disconnections

Capabilities

Server Creation

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;

Message Handler Binding

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

Payload Processing

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 };

Server Management

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>;

Inherited Connection Management

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;

Types

/**
 * 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;
}