Engine.IO Realtime Engine

6.6.6 · active · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up an Engine.IO server, attach it to an existing Node.js HTTP server, and handle client connections, messages, and disconnections. It includes basic error handling and illustrates both sending and receiving data.

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

view raw JSON →