Skip to content

Remote comms: Add TCP transport support for Node.js connections #645

@sirtimid

Description

@sirtimid

Problem

Currently only WebSocket/WebRTC transports are configured in connection-factory.ts, limiting Node.js environments to relay-based connections. Direct TCP connections would provide better performance for server-to-server communication.

Expected Behavior

Enable direct TCP connections for remote comms between Node.js instances to bypass relay requirements.

Implementation

Files to Modify

File Changes
platform/connection-factory.ts Add TCP transport conditionally for Node.js

Approach

  1. Detect runtime environment

    • Check if running in Node.js vs browser
    • Use environment detection (e.g., typeof process \!== 'undefined')
  2. Add TCP transport for Node.js (in ConnectionFactory.#init())

    import { tcp } from '@libp2p/tcp';
    
    // In transports array:
    ...(isNodeJS ? [tcp()] : []),
  3. Add TCP listening addresses for Node.js

    addresses: {
      listen: [
        '/webrtc',
        '/p2p-circuit',
        ...(isNodeJS ? ['/ip4/0.0.0.0/tcp/0'] : []),
      ],
    }
  4. Update candidateAddressStrings()

    • Try direct TCP addresses before relay paths when available
    • Prioritize direct connections over relayed ones

Dependencies

  • @libp2p/tcp package

Acceptance Criteria

  • TCP transport loaded when running in Node.js
  • TCP listening address configured in Node.js mode
  • Direct TCP connections attempted before relay fallback
  • Browser environments continue to work (WebSocket/WebRTC only)
  • Unit tests verify transport configuration per environment
  • E2E test for direct TCP connection between Node.js instances

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions