Engine.IO Client

6.6.4 · active · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

Demonstrates a basic Engine.IO client and server interaction, sending a message and receiving an echo.

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);
    });
  });
});

view raw JSON →