or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

configuration.mdindex.mdmiddleware.md
tile.json

configuration.mddocs/

Socket.io Configuration

Core configuration functionality for integrating Socket.io with Feathers applications, providing flexible setup options and server customization.

Capabilities

Configure Socket.io Function

Main configuration function that integrates Socket.io with Feathers applications. Supports multiple overloaded signatures to accommodate different configuration patterns.

/**
 * Configure Socket.io integration with Feathers application
 * @param callback - Optional configuration callback for the io instance
 * @returns Feathers application configurator function
 */
function configureSocketio(callback?: (io: Server) => void): (app: Application) => void;

/**
 * Configure Socket.io with options or port
 * @param options - Port number or Socket.io server options
 * @param callback - Optional configuration callback for the io instance
 * @returns Feathers application configurator function
 */
function configureSocketio(
  options: number | Partial<ServerOptions>,
  callback?: (io: Server) => void
): (app: Application) => void;

/**
 * Configure Socket.io with explicit port and options
 * @param port - Port number for the Socket.io server
 * @param options - Socket.io server options
 * @param callback - Optional configuration callback for the io instance
 * @returns Feathers application configurator function
 */
function configureSocketio(
  port: number,
  options?: Partial<ServerOptions>,
  callback?: (io: Server) => void
): (app: Application) => void;

Usage Examples:

import { feathers } from "@feathersjs/feathers";
import socketio = require("@feathersjs/socketio");

const app = feathers();

// Basic configuration with no parameters
app.configure(socketio());

// Configuration with callback only
app.configure(socketio((io) => {
  io.sockets.setMaxListeners(100);
}));

// Configuration with port number
app.configure(socketio(3030));

// Configuration with options object
app.configure(socketio({
  cors: {
    origin: "*",
    methods: ["GET", "POST"]
  },
  transports: ["websocket", "polling"]
}));

// Configuration with port and options
app.configure(socketio(3030, {
  cors: { origin: "*" }
}));

// Full configuration with port, options, and callback
app.configure(socketio(3030, {
  cors: { origin: "*" }
}, (io) => {
  io.sockets.setMaxListeners(64);
}));

Application Methods Extension

The configuration function extends the Feathers Application with Socket.io-specific methods:

interface Application<Services, Settings> {
  /** Socket.io server instance */
  io: any;
  
  /**
   * Enhanced listen method that works with both Express and standalone servers
   * @param options - Listen options (port, host, etc.)
   * @returns Promise resolving to HTTP server instance
   */
  listen(options: any): Promise<http.Server>;
}

Setup Process

When the application is configured with Socket.io, the following setup occurs:

  1. Server Creation: If no existing listen method, creates an HTTP server
  2. Socket.io Integration: Attaches Socket.io server to the HTTP server or specified port
  3. Middleware Setup: Automatically configures built-in middleware:
    • Disconnect handling for connection cleanup
    • Parameter extraction from socket handshake
    • Authentication integration with Feathers auth system
  4. Event Listener Configuration: Sets up appropriate event listener limits (default: 64)
  5. Callback Execution: Runs user-provided configuration callback if specified

Integration with Feathers Transport Commons

The configuration leverages @feathersjs/transport-commons for:

  • Real-time connection mapping
  • Service method routing over sockets
  • Event emission for service events
  • Parameter handling and validation

Server Options

Supports all Socket.io ServerOptions including:

interface ServerOptions {
  cors?: {
    origin?: string | string[] | boolean;
    methods?: string[];
    allowedHeaders?: string[];
    credentials?: boolean;
  };
  transports?: string[];
  path?: string;
  serveClient?: boolean;
  adapter?: any;
  parser?: any;
  connectTimeout?: number;
  pingTimeout?: number;
  pingInterval?: number;
  maxHttpBufferSize?: number;
  allowRequest?: Function;
  cookie?: any;
  // ... other Socket.io options
}