or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

configuration.mdindex.mdmiddleware.md
tile.json

middleware.mddocs/

Internal Architecture Details

Internal middleware functions for handling socket connections, parameter extraction, and authentication within the Feathers ecosystem. These functions are automatically configured when using configureSocketio(). While the functions themselves are internal, their types and interfaces are available for advanced usage scenarios.

Capabilities

Disconnect Middleware

Handles socket disconnection events and manages the connection lifecycle between Socket.io sockets and Feathers real-time connections.

/**
 * Creates middleware for handling socket disconnection events
 * @param app - Feathers application instance
 * @param getParams - Function to extract parameters from socket
 * @param socketMap - WeakMap for tracking socket-to-connection mapping
 * @returns Socket middleware function
 */
function disconnect(
  app: Application,
  getParams: ParamsGetter,
  socketMap: WeakMap<RealTimeConnection, FeathersSocket>
): (socket: FeathersSocket, next: NextFunction) => void;

Functionality:

  • Listens for application disconnect events and closes corresponding sockets
  • Registers socket disconnect handlers that emit Feathers disconnect events
  • Manages cleanup of socket connections when they close

Usage Example:

import { disconnect } from "@feathersjs/socketio/middleware";

const getParams = (socket) => socket.feathers;
const socketMap = new WeakMap();

io.use(disconnect(app, getParams, socketMap));

Parameters Middleware

Sets up socket connection parameters by extracting information from the Socket.io handshake and creating Feathers-compatible parameter objects.

/**
 * Creates middleware for setting up socket connection parameters
 * @param _app - Feathers application instance (unused in implementation)
 * @param socketMap - WeakMap for tracking socket-to-connection mapping
 * @returns Socket middleware function
 */
function params(
  _app: Application,
  socketMap: WeakMap<RealTimeConnection, FeathersSocket>
): (socket: FeathersSocket, next: NextFunction) => void;

Functionality:

  • Extracts headers from socket handshake
  • Sets provider to 'socketio' for Feathers service identification
  • Creates socket.feathers object with connection parameters
  • Maps socket to real-time connection for tracking

Parameters Set:

socket.feathers = {
  provider: 'socketio',
  headers: socket.handshake.headers
};

Authentication Middleware

Integrates Socket.io connections with the Feathers authentication system, parsing authentication data from socket handshakes.

/**
 * Creates middleware for handling socket authentication
 * @param app - Feathers application instance
 * @param getParams - Function to extract parameters from socket
 * @param settings - Optional authentication settings
 * @returns Socket middleware function
 */
function authentication(
  app: Application,
  getParams: ParamsGetter,
  settings?: any
): (socket: FeathersSocket, next: NextFunction) => void;

Functionality:

  • Integrates with Feathers authentication service
  • Parses authentication strategies from socket handshake
  • Handles authentication token validation
  • Creates authenticated connection context

Authentication Flow:

  1. Checks for default authentication service on the app
  2. Retrieves configured authentication strategies
  3. Parses authentication data from socket handshake
  4. Validates authentication using configured strategies
  5. Sets up authenticated connection context

Usage Example:

import { authentication } from "@feathersjs/socketio/middleware";

const getParams = (socket) => socket.feathers;
const authSettings = { service: 'authentication' };

io.use(authentication(app, getParams, authSettings));

Types and Interfaces

FeathersSocket Interface

Extended Socket.io socket with Feathers-specific properties for parameter storage and connection tracking.

/**
 * Extended Socket.io socket with Feathers-specific properties
 */
interface FeathersSocket extends Socket {
  /** Feathers connection parameters and metadata */
  feathers?: Params & { [key: string]: any };
}

ParamsGetter Type

Function type for extracting parameters from a socket connection.

/**
 * Function type for extracting parameters from socket
 * @param socket - Socket.io socket instance
 * @returns Parameters object
 */
type ParamsGetter = (socket: Socket) => any;

NextFunction Type

Standard middleware next function for continuing the middleware chain.

/**
 * Middleware next function type
 * @param err - Optional error to pass to error handling
 */
type NextFunction = (err?: any) => void;

Middleware Chain Order

The middleware functions are automatically applied in the following order during Socket.io configuration:

  1. Disconnect Middleware: Sets up connection cleanup handlers
  2. Parameters Middleware: Extracts and sets up connection parameters
  3. Authentication Middleware: Handles authentication parsing and validation

This order ensures proper parameter setup before authentication and proper cleanup handling throughout the connection lifecycle.

Integration with Socket.io

These middleware functions integrate seamlessly with Socket.io's middleware system:

// Automatic integration during configuration
io.use(disconnect(app, getParams, socketMap));
io.use(params(app, socketMap));
io.use(authentication(app, getParams));

Error Handling

All middleware functions support standard Socket.io error handling:

  • Errors passed to next(error) will prevent connection establishment
  • Authentication errors are properly propagated
  • Connection cleanup is handled gracefully on errors