CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-engine-io-client

Client for the realtime Engine providing bidirectional communication with WebSocket, polling, and WebTransport support

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

socket-connection.mddocs/

Socket Connection Management

Core socket functionality for establishing and managing real-time bidirectional connections with support for multiple transport protocols and automatic upgrading capabilities.

Capabilities

Socket Class

Main Socket class providing full transport upgrade support with default transports (polling, websocket, webtransport).

/**
 * Main Socket class with full transport upgrade support and default transports
 * Automatically handles transport selection and upgrading
 */
class Socket extends SocketWithUpgrade {
  constructor(uri?: string, opts?: SocketOptions);
  constructor(opts: SocketOptions);
  constructor(uri?: string | SocketOptions, opts?: SocketOptions);
  
  // Inherited public properties
  id: string;
  transport: Transport;
  binaryType: BinaryType;
  readyState: SocketState;
  writeBuffer: Packet[];
  
  // Inherited public methods
  send(msg: RawData, options?: WriteOptions, fn?: () => void): Socket;
  write(msg: RawData, options?: WriteOptions, fn?: () => void): Socket;
  close(): Socket;
  
  // Static properties
  static protocol: number;
  static priorWebsocketSuccess: boolean;
}

Usage Examples:

import { Socket } from "engine.io-client";

// Connect with URI and options
const socket = new Socket("wss://api.example.com", {
  transports: ["websocket", "polling"],
  query: { token: "abc123" }
});

// Connect with options only
const socket = new Socket({
  hostname: "localhost",
  port: 3000,
  secure: true,
  path: "/socket.io/"
});

SocketWithUpgrade Class

Socket class with transport upgrade capabilities but without default transports, suitable for custom transport configurations.

/**
 * Socket class with transport upgrade capabilities
 * Requires explicit transport configuration
 */
class SocketWithUpgrade extends SocketWithoutUpgrade {
  // Inherits all SocketWithoutUpgrade properties and methods
  // Adds automatic transport upgrading functionality
}

SocketWithoutUpgrade Class

Base socket class without upgrade mechanism, ideal for tree-shaking and when upgrade functionality is not needed.

/**
 * Socket class without upgrade mechanism for tree-shaking support
 * Maintains connection with the first successful transport
 */
class SocketWithoutUpgrade extends Emitter<
  Record<never, never>,
  Record<never, never>,
  SocketReservedEvents
> {
  constructor(uri: string | BaseSocketOptions, opts: BaseSocketOptions);
  
  // Public properties
  id: string;
  transport: Transport;
  binaryType: BinaryType;
  readyState: SocketState;
  writeBuffer: Packet[];
  
  // Public methods
  write(msg: RawData, options?: WriteOptions, fn?: () => void): Socket;
  send(msg: RawData, options?: WriteOptions, fn?: () => void): Socket;
  close(): Socket;
  
  // Static properties
  static priorWebsocketSuccess: boolean;
  static protocol: number;
}

interface BaseSocketOptions extends Omit<SocketOptions, "transports"> {
  transports: TransportCtor[];
}

type TransportCtor = { new (o: any): Transport };

Usage Examples:

import { SocketWithoutUpgrade, WebSocket } from "engine.io-client";

// Create socket with specific transport only
const socket = new SocketWithoutUpgrade({
  hostname: "localhost",
  port: 3000,
  transports: [WebSocket]
});

Send and Write Methods

Send data through the established connection with optional compression and callback support.

/**
 * Send data through the socket connection
 * @param msg - Data to send (string, Buffer, ArrayBuffer, etc.)
 * @param options - Optional write options
 * @param fn - Optional callback function
 * @returns Socket instance for method chaining
 */
write(msg: RawData, options?: WriteOptions, fn?: () => void): Socket;

/**
 * Alias for write method
 * @param msg - Data to send
 * @param options - Optional write options  
 * @param fn - Optional callback function
 * @returns Socket instance for method chaining
 */
send(msg: RawData, options?: WriteOptions, fn?: () => void): Socket;

interface WriteOptions {
  compress?: boolean;
}

type RawData = string | Buffer | ArrayBuffer | ArrayBufferView | Blob;

Usage Examples:

// Send string data
socket.send("Hello server!");

// Send JSON data
socket.send(JSON.stringify({ type: "message", content: "Hello" }));

// Send binary data
const buffer = new ArrayBuffer(8);
socket.send(buffer);

// Send with compression
socket.send(largeString, { compress: true });

// Send with callback
socket.send("data", undefined, () => {
  console.log("Data sent successfully");
});

Close Method

Gracefully close the socket connection and cleanup resources.

/**
 * Close the socket connection
 * Triggers 'close' event when complete
 * @returns Socket instance for method chaining
 */
close(): Socket;

Usage Examples:

// Close connection
socket.close();

// Listen for close event
socket.on("close", (reason, description) => {
  console.log("Connection closed:", reason);
});

Socket State Management

Monitor and access current connection state and properties.

type SocketState = "opening" | "open" | "closing" | "closed";
type BinaryType = "blob" | "arraybuffer";

interface Packet {
  type: PacketType;
  data?: RawData;
}

Usage Examples:

// Check connection state
if (socket.readyState === "open") {
  socket.send("data");
}

// Set binary data format
socket.binaryType = "arraybuffer";

// Access socket ID (available after handshake)
console.log("Socket ID:", socket.id);

// Check current transport
console.log("Using transport:", socket.transport.name);

// Monitor write buffer
console.log("Queued packets:", socket.writeBuffer.length);

Types

interface SocketReservedEvents {
  open: () => void;
  handshake: (data: HandshakeData) => void;
  packet: (packet: Packet) => void;
  packetCreate: (packet: Packet) => void;
  data: (data: RawData) => void;
  message: (data: RawData) => void;
  drain: () => void;
  flush: () => void;
  heartbeat: () => void;
  ping: () => void;
  pong: () => void;
  error: (err: string | Error) => void;
  upgrading: (transport: Transport) => void;
  upgrade: (transport: Transport) => void;
  upgradeError: (err: Error) => void;
  close: (reason: string, description?: CloseDetails | Error) => void;
}

interface HandshakeData {
  sid: string;
  upgrades: string[];
  pingInterval: number;
  pingTimeout: number;
  maxPayload: number;
}

Install with Tessl CLI

npx tessl i tessl/npm-engine-io-client

docs

event-handling.md

index.md

socket-connection.md

transport-system.md

utility-functions.md

tile.json