Thanks to visit codestin.com
Credit goes to deepwiki.com

Menu

Socket.IO and WebSocket Communication

Relevant source files

Purpose and Scope

This document describes the bidirectional communication infrastructure in Trigger.dev, covering both Socket.IO and native WebSocket implementations. Socket.IO is used primarily for worker coordination and control plane communication, while native WebSockets serve specific protocol requirements. For unidirectional real-time updates to the dashboard, see Realtime Updates and SSE.

Related pages:


Overview of Communication Systems

Trigger.dev employs two distinct bidirectional communication mechanisms, each serving different architectural requirements:

TechnologyUse CasePathApplications
Socket.IOWorker coordination, control plane/socket.io/*Coordinator, Supervisor, Webapp
Native WebSocketDirect protocol communication/wsWebapp

Both systems operate on the same Express HTTP server and share connection upgrade handling logic but serve fundamentally different purposes in the system architecture.

Sources: apps/webapp/server.ts1-269


Architecture Overview

Sources: apps/webapp/server.ts228-263 apps/webapp/app/entry.server.tsx234-235


Socket.IO Configuration

Server Initialization

Socket.IO is initialized in the application entry point and attached to the Express server during startup. The server instance is exported from the build module and made available to the HTTP server setup code.

The Socket.IO server is extracted from the build entry module and attached to the HTTP server:

apps/webapp/server.ts120-121

apps/webapp/server.ts225

Sources: apps/webapp/server.ts120-121 apps/webapp/server.ts225 apps/webapp/app/entry.server.tsx234

Redis Adapter for Multi-Instance Coordination

Socket.IO uses a Redis adapter (@socket.io/redis-adapter) to enable message broadcasting across multiple server instances in clustered deployments. This is critical for horizontal scaling where Socket.IO clients may connect to different server instances.

Package Dependencies:

  • socket.io: 4.7.4
  • socket.io-adapter: 2.5.4
  • @socket.io/redis-adapter: 8.3.0

Environment Configuration:

VariableDefaultPurpose
PUBSUB_REDIS_HOSTFalls back to REDIS_HOSTRedis host for pub/sub
PUBSUB_REDIS_PORTFalls back to REDIS_PORTRedis port
PUBSUB_REDIS_USERNAMEFalls back to REDIS_USERNAMEAuthentication username
PUBSUB_REDIS_PASSWORDFalls back to REDIS_PASSWORDAuthentication password
PUBSUB_REDIS_TLS_DISABLEDfalseWhether to disable TLS
PUBSUB_REDIS_CLUSTER_MODE_ENABLED0Enable Redis cluster mode

Sources: apps/webapp/package.json110 apps/webapp/package.json200-201 apps/webapp/app/env.server.ts207-237 pnpm-lock.yaml432-434


Native WebSocket Configuration

WebSocket Server Setup

The native WebSocket implementation uses the ws library (version 8.12.0) for direct WebSocket protocol support without the Socket.IO protocol overhead.

The WebSocket server is instantiated separately from Socket.IO:

apps/webapp/server.ts12

apps/webapp/server.ts121

The handlers are exported from a separate module:

apps/webapp/app/entry.server.tsx235

Sources: apps/webapp/server.ts12 apps/webapp/server.ts121 apps/webapp/app/entry.server.tsx235 apps/webapp/package.json213


Connection Upgrade Mechanism

Both Socket.IO and native WebSocket connections begin as HTTP requests that are upgraded to the WebSocket protocol. The Express server implements a custom upgrade event handler to route connections based on the request path.

Upgrade Flow Diagram

Implementation Details

The upgrade handler is registered on the Express HTTP server after removing default listeners added by Socket.IO:

apps/webapp/server.ts226

apps/webapp/server.ts228-263

Key Routing Logic:

  1. Socket.IO Path (/socket.io/*): Delegates to Engine.IO's handleUpgrade method
  2. WebSocket Path (/ws): Delegates to ws library's handleUpgrade method
  3. Invalid Path: Destroys the socket with an error message

Sources: apps/webapp/server.ts226-263


Clustering and Multi-Process Support

Cluster Mode Configuration

The webapp supports optional clustering to distribute load across multiple CPU cores. When clustering is enabled, each worker process runs its own Express server with Socket.IO and WebSocket instances.

Environment Variables:

VariablePurpose
ENABLE_CLUSTERSet to "1" to enable clustering
WEB_CONCURRENCY or CLUSTER_WORKERSNumber of worker processes (defaults to CPU count)
GRACEFUL_SHUTDOWN_TIMEOUTMilliseconds to wait for graceful shutdown (default: 30000)

Cluster Architecture

Primary Process Responsibilities

apps/webapp/server.ts18-28

The primary process:

  1. Forks worker processes based on configuration
  2. Monitors worker lifecycle and replaces crashed workers
  3. Forwards signals (SIGTERM, SIGINT) to all workers
  4. Coordinates graceful shutdown across the cluster

apps/webapp/server.ts73-89

Worker Process Title

Each worker process sets a descriptive title for easier identification in process monitoring tools:

apps/webapp/server.ts109-111

Sources: apps/webapp/server.ts15-90 apps/webapp/server.ts109-111


Handler Implementation Structure

The actual Socket.IO and WebSocket connection handling logic is implemented in separate modules that are referenced but not directly visible in the provided source files.

Handler Modules

ModuleExportPurpose
./v3/handleSocketIo.serversocketIo: { io: IoServer }Socket.IO server instance and handlers
./v3/handleWebsockets.serverwss: WebSocketServerWebSocket server instance and handlers

These modules are exported from the entry server and consumed by the main server setup:

apps/webapp/app/entry.server.tsx234-235

The server extracts these exports from the build module:

apps/webapp/server.ts115

apps/webapp/server.ts120-121

Sources: apps/webapp/app/entry.server.tsx234-235 apps/webapp/server.ts115 apps/webapp/server.ts120-121


Integration with Other Services

Coordinator and Supervisor Applications

Socket.IO is not limited to the webapp; it's also used in other applications within the Trigger.dev architecture:

Coordinator App apps/coordinator/package.json:

  • Uses socket.io: 4.7.4
  • Coordinates worker communication

Supervisor App apps/supervisor/package.json:

  • Uses socket.io: 4.7.4
  • Manages worker lifecycle and communication

Communication Flow

Sources: pnpm-lock.yaml99-125 pnpm-lock.yaml168-197


Engine.IO Parser Patch

The project includes a patch for [email protected], which is the underlying protocol parser for Socket.IO. This patch modifies the parsing behavior to address specific issues.

Patch Location: patches/[email protected]

Patch Reference in Dependencies:

pnpm-lock.yaml26-28

This patch is automatically applied during pnpm install and affects all Socket.IO communication in the system.

Sources: pnpm-lock.yaml26-28 package.json79


Server Configuration and Lifecycle

HTTP Server Settings

The Express HTTP server is configured with specific settings to optimize Socket.IO and WebSocket communication:

apps/webapp/server.ts201-205

Graceful Shutdown

The server implements graceful shutdown for both HTTP and WebSocket connections:

apps/webapp/server.ts207-223

In cluster mode, the primary process coordinates graceful shutdown across all workers:

apps/webapp/server.ts45-57

Sources: apps/webapp/server.ts201-223 apps/webapp/server.ts45-57


Error Handling

Socket Error Management

The upgrade handler includes error handling for socket-level failures:

apps/webapp/server.ts231-233

Path Validation

Invalid upgrade requests receive explicit error messages:

apps/webapp/server.ts246-255

Sources: apps/webapp/server.ts231-255


Deployment Considerations

Docker Configuration

The Docker build includes all necessary dependencies for Socket.IO and WebSocket support:

docker/Dockerfile1

The production image includes all runtime dependencies, including Socket.IO and WebSocket libraries, as specified in the production dependencies phase.

Port Configuration

The server listens on a configurable port:

apps/webapp/server.ts117

docker/Dockerfile105

Sources: docker/Dockerfile1 docker/Dockerfile105 apps/webapp/server.ts117


Summary

The Trigger.dev Socket.IO and WebSocket infrastructure provides:

  1. Dual Protocol Support: Socket.IO for rich bidirectional communication with fallback mechanisms, and native WebSockets for direct protocol access
  2. Redis-based Clustering: Horizontal scaling support via Redis adapter for Socket.IO
  3. Path-based Routing: Intelligent upgrade handler routes connections to appropriate protocol handlers
  4. Graceful Lifecycle Management: Proper shutdown handling in both single-instance and clustered deployments
  5. Multi-Application Architecture: Consistent Socket.IO usage across webapp, coordinator, and supervisor services

The implementation separates transport concerns (handled in server.ts) from business logic (handled in handleSocketIo.server.ts and handleWebsockets.server.ts), providing a clean separation of concerns and maintainable architecture.

Sources: apps/webapp/server.ts1-269 apps/webapp/app/entry.server.tsx234-235 apps/webapp/package.json1-290