Engine.IO Realtime Engine
Engine.IO is a foundational real-time communication library that underpins Socket.IO, providing the bidirectional connection layer between client and server. It offers robust transport mechanisms to ensure maximum reliability across various network conditions, including proxies, load balancers, and personal firewall software. Currently stable at version 6.6.6, Engine.IO receives updates in alignment with the broader Socket.IO ecosystem, often focusing on bug fixes, dependency updates, and security patches, as seen in recent releases. Its design prioritizes minimal client size and scalability, making it suitable for high-performance applications. Unlike higher-level abstractions, Engine.IO exposes a 100% Node.js core-style API, emphasizing a direct and unopinionated approach to real-time communication without extensive API sugar, allowing developers granular control over the connection lifecycle and underlying transports.
Common errors
-
ERR_REQUIRE_ESM: require() of ES Module path/to/node_modules/engine.io/build/index.mjs not supported.
cause Attempting to use `require('engine.io')` in an ES Module environment (`"type": "module"` in `package.json`) that does not permit CommonJS `require()` calls for ESM packages, or vice-versa.fixIf your project is an ESM project, use `import { Server } from 'engine.io';`. If it's a CJS project, ensure `"type": "commonjs"` is set in `package.json` (or remove `type` field) and use `const { Server } = require('engine.io');`. -
TypeError: engine.Server is not a constructor
cause Incorrectly importing the `Server` class, often by attempting to use `import engine from 'engine.io'; new engine.Server();` in an ESM project, or `new Server()` when `Server` was not correctly destructured from a CommonJS `require()`.fixIf using ES Modules, ensure you're using named imports: `import { Server } from 'engine.io';` then `new Server()`. If using CommonJS, use `const { Server } = require('engine.io');` or `const engine = require('engine.io'); new engine.Server();`. -
WebSocket connection to 'ws://localhost:3000/' failed: Error during WebSocket handshake: Unexpected response code: 400
cause The client attempted to connect via WebSocket, but the server was either not running, not properly configured to handle the WebSocket upgrade request, or there's a proxy/firewall interfering. This often happens if the `engine.io` server is not attached to an HTTP server, or the path is incorrect.fixVerify that the `engine.io` server is running and successfully attached to an `http.Server` instance. Ensure the client's connection URL (e.g., `ws://localhost:3000`) correctly points to the server's address and port. Check any reverse proxy configurations or firewall rules that might block WebSocket connections.
Warnings
- breaking A critical security vulnerability (CVE-2026-33151) affecting `socket.io-parser` has been patched across several `socket.io-parser` versions (>=3.3.5, >=3.4.4, >=4.2.6). While `engine.io` itself does not directly depend on `socket.io-parser`, this module is a fundamental component of the broader Socket.IO ecosystem, including setups where `engine.io` is used as a foundation for `socket.io`. The vulnerability addresses a limit on binary attachments to prevent potential denial-of-service or memory exhaustion attacks. Users running `socket.io` should ensure their `socket.io-parser` dependency is updated to mitigate this risk.
- gotcha Node.js's `url.parse()` function has been deprecated in favor of the `new URL()` constructor. While `engine.io`'s internal handling should be updated, applications leveraging `engine.io` that manually parse URLs using `url.parse()` should migrate to `new URL()` to avoid deprecation warnings and ensure future compatibility, especially with newer Node.js versions.
- gotcha The `engine.io` library ships with both CommonJS (CJS) and ES Module (ESM) exports. The provided README examples predominantly use CJS `require()`. When integrating into an ES Module project, direct named `import { Server } from 'engine.io';` should be used. Mixing `require()` with `import` statements or incorrect import paths in ESM projects can lead to 'ERR_REQUIRE_ESM' or 'default is not a constructor' errors.
Install
-
npm install engine.io -
yarn add engine.io -
pnpm add engine.io
Imports
- Server
const Server = require('engine.io').Server;import { Server } from 'engine.io'; - listen
const listen = require('engine.io').listen;import { listen } from 'engine.io'; - Socket
const Socket = require('engine.io').Socket;import { Socket } from 'engine.io';
Quickstart
import { Server } from 'engine.io';
import * as http from 'http';
// Create a basic HTTP server
const httpServer = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Engine.IO is running!\n');
});
// Attach Engine.IO to the HTTP server
const eioServer = new Server({
pingInterval: 1000, // Client pings server every 1 second
pingTimeout: 500 // Server considers client disconnected if no ping for 0.5 seconds
});
eioServer.attach(httpServer);
// Handle connections
eioServer.on('connection', (socket) => {
console.log(`Client connected: ${socket.id}`);
// Send a message to the client
socket.send('Hello from Engine.IO server!');
// Listen for messages from the client
socket.on('message', (data) => {
console.log(`Received message from ${socket.id}: ${data}`);
// Echo the message back to the client
socket.send(`Server received: ${data}`);
});
// Handle client disconnection
socket.on('close', (reason, description) => {
console.log(`Client ${socket.id} disconnected. Reason: ${reason}, Description: ${description}`);
});
// Handle errors
socket.on('error', (err) => {
console.error(`Error on socket ${socket.id}:`, err);
});
});
// Start the HTTP server
const PORT = process.env.PORT || 3000;
httpServer.listen(PORT, () => {
console.log(`Engine.IO server listening on http://localhost:${PORT}`);
console.log('Connect with an Engine.IO client, e.g., in browser: new eio.Socket(\'ws://localhost:3000\');');
});