startTcpTunnel()
Starts a persistent TCP → WebSocket proxy. The function creates a local TCP server that listens on an ephemeral port on 127.0.0.1. When a TCP client connects, it forwards all traffic between that client andremoteURL through an authenticated WebSocket. The server remains active even after the client disconnects, allowing reconnection without recreating the tunnel.
Parameters
Remote WebSocket endpoint (e.g. wss://example.com/instance)
Bearer token sent as
Authorization headerIP address to listen on. Use ‘127.0.0.1’ for localhost.
Port number to listen on. Use 0 to ask Node.js to find an available non-privileged port.
Tunnel configuration options
Maximum number of reconnection attempts
Initial reconnection delay in milliseconds
Maximum reconnection delay in milliseconds
Controls logging verbosity. One of:
'none', 'error', 'warn', 'info', 'debug'Tunnel mode for handling TCP connections:
'singleton': Single TCP connection forwarded to WebSocket (default)'multiplexed': Multiple TCP connections multiplexed over a single WebSocket, each packet prefixed with a 4-byte connection ID
Returns
A promise that resolves to a Tunnel object
Examples
Tunnel
Represents an active TCP tunnel.Properties
Methods
close()
Close the tunnel and stop accepting connections.getConnectionState()
Get current WebSocket connection state.One of:
'connecting', 'connected', 'disconnected', 'reconnecting'onConnectionStateChange()
Register callback for WebSocket connection state changes.Callback function called when connection state changes
A function to unregister the callback
Tunnel Modes
Singleton Mode
In singleton mode (default), the tunnel accepts one TCP connection at a time. Each connection gets its own WebSocket session. Use cases:- ADB connections
- SSH tunnels
- Simple proxying scenarios
- Only one TCP client can connect at a time
- New connections are rejected while an existing connection is active
- WebSocket reconnects automatically on network issues
- TCP connection is maintained across WebSocket reconnects using a session ID
Multiplexed Mode
In multiplexed mode, multiple TCP connections are multiplexed over a single WebSocket connection. Each TCP connection is assigned a unique 32-bit connection ID, and all data is prefixed with a 4-byte big-endian header. Use cases:- Multiple simultaneous connections
- Scenarios where WebSocket connection overhead matters
- High-throughput applications
- Multiple TCP clients can connect simultaneously
- All connections share a single WebSocket
- Each packet is prefixed with a 4-byte connection ID header
- Close signals are sent as header-only packets (4 bytes, no payload)
Types
TcpTunnelOptions
TunnelMode
'singleton': Single TCP connection forwarded to WebSocket (default)'multiplexed': Multiple TCP connections multiplexed over a single WebSocket