Engine.IO Client
Engine.IO Client is the low-level JavaScript client for Engine.IO, providing the foundational transport-based, bidirectional communication layer that powers Socket.IO. As of version 6.6.4, it supports various transports like HTTP long-polling and WebSockets, with recent updates also introducing WebTransport support and features like transport tree-shaking for optimized bundles. The library is actively maintained with regular updates, often coinciding with releases of its server-side counterpart, Engine.IO, and the Socket.IO framework. It differentiates itself by offering a robust, transport-agnostic real-time communication channel, handling connection upgrades and downgrades automatically to ensure persistent connectivity across different network conditions and environments. While it can be used independently for raw real-time communication, it's primarily designed as the underlying mechanism for the more feature-rich Socket.IO client.
Common errors
-
Error: Cannot find module 'engine.io-client'
cause The package is not installed or the path is incorrect.fixRun `npm install engine.io-client` or `yarn add engine.io-client` to install the package. Verify the import path is correct. -
TypeError: (0 , engine_io_client__WEBPACK_IMPORTED_MODULE_0__.Socket) is not a constructor
cause Attempting to use `Socket` as a default import when it is a named export, common in ESM environments.fixChange `import Socket from 'engine.io-client';` to `import { Socket } from 'engine.io-client';` -
ReferenceError: eio is not defined
cause The `eio` global function is only available when using the standalone `engine.io.js` browser bundle directly, not when importing the npm package in a module environment.fixIf using npm and a bundler, `import { Socket } from 'engine.io-client';` is the correct approach. If targeting browsers without a module bundler, ensure `engine.io.js` is loaded via a `<script>` tag before your application code. -
RangeError: Maximum call stack size exceeded
cause This can sometimes occur with large binary data transfers or very rapid message sending, especially if `socket.io-parser` is an older, vulnerable version.fixUpgrade `engine.io-client` and its transitive dependencies, particularly `socket.io-parser`, to the latest versions to benefit from fixes like the binary attachment limit (CVE-2026-33151). Consider implementing flow control or message throttling on the application layer for very high-volume data streams.
Warnings
- breaking Engine.IO v4 (and subsequently Socket.IO v3) introduced significant breaking changes including a reversal of the heartbeat mechanism (server now sends ping) and changes to packet encoding. Clients from v3 will not be able to connect to v4 servers and vice-versa.
- breaking The `perMessageDeflate` WebSocket option is now disabled by default since Engine.IO v4 to prevent excessive memory usage in production deployments.
- breaking A critical vulnerability (CVE-2026-33151) in `socket.io-parser` (a core dependency/peer of the Socket.IO ecosystem) allows a specially crafted packet to cause server memory exhaustion, leading to Denial of Service. While `engine.io-client` itself doesn't directly implement the parser, it's crucial to upgrade all related Socket.IO components.
- gotcha When using `extraHeaders` for authentication or custom data, these headers are only sent during HTTP polling requests in the browser, not during WebSocket upgrade requests due to browser WebSocket API limitations.
- gotcha Although `engine.io-client` can technically be used without Socket.IO, it is generally not recommended for most application development. Engine.IO provides only the basic transport layer, lacking features like multiplexing, automatic reconnection, and acknowledgment callbacks that Socket.IO offers.
Install
-
npm install engine.io-client -
yarn add engine.io-client -
pnpm add engine.io-client
Imports
- Socket
import Socket from 'engine.io-client'; // Socket is a named export, not default
import { Socket } from 'engine.io-client'; - Socket
const Socket = require('engine.io-client'); // Returns an object with Socket as a propertyconst { Socket } = require('engine.io-client'); - eio
import { eio } from 'engine.io-client'; // 'eio' is a global in standalone build, not an export<script src="/path/to/engine.io.js"></script> <script> const socket = eio('ws://localhost'); </script> - SocketWithoutUpgrade, XHR, WebSocket
import { SocketWithoutUpgrade, XHR, WebSocket } from 'engine.io-client';
Quickstart
import { Socket } from 'engine.io-client';
import { Server } from 'engine.io';
import http from 'http';
const httpServer = http.createServer();
const eioServer = new Server(httpServer);
eioServer.on('connection', (serverSocket) => {
console.log(`Server: Client connected: ${serverSocket.id}`);
serverSocket.on('message', (data) => {
console.log(`Server: Received message from ${serverSocket.id}: ${data}`);
serverSocket.send(`Echo: ${data}`);
});
serverSocket.on('close', (reason, description) => {
console.log(`Server: Client ${serverSocket.id} disconnected: ${reason} - ${description}`);
});
});
httpServer.listen(3000, () => {
console.log('Engine.IO server listening on port 3000');
// Client-side code
const clientSocket = new Socket('ws://localhost:3000');
clientSocket.on('open', () => {
console.log('Client: Connection opened!');
clientSocket.send('Hello from client!');
clientSocket.on('message', (data) => {
console.log(`Client: Received message: ${data}`);
if (data === 'Echo: Hello from client!') {
clientSocket.close();
}
});
clientSocket.on('close', () => {
console.log('Client: Connection closed!');
});
clientSocket.on('error', (err) => {
console.error('Client: Error!', err);
});
});
});