Client

WebSocket Tunnel

Learn how to start a WebSocket tunnel with the Portr CLI to expose local real-time apps and WebSocket servers to the public internet.

WebSocket connections use the HTTP protocol for the initial handshake and then switch to use a TCP connection. Because of this, an HTTP tunnel will work perfectly for WebSocket connections.

portr http 9000

With a fixed subdomain:

portr http 9000 -s realtime-dev

Use the HTTP tunnel command for WebSocket services. The initial HTTP handshake will be handled properly, and the connection will upgrade to WebSocket as expected.

The Portr client-to-server tunnel transport is selected by your server-generated config. It connects to ssh_url when transport: ssh, or to ws_url when transport: websocket. Public HTTP and application WebSocket traffic still uses the normal tunnel URL such as https://realtime-dev.example.com or wss://realtime-dev.example.com.

How WebSocket Tunneling Works

  1. Initial handshake: The WebSocket connection starts with an HTTP request containing upgrade headers
  2. Protocol upgrade: The server responds with a 101 Switching Protocols status
  3. WebSocket connection: The connection upgrades to WebSocket protocol over the existing TCP connection
  4. Bidirectional communication: Full-duplex communication is established
  5. Local logging: The inspector records the handshake and WebSocket frame events in the local SQLite dashboard database

Testing WebSocket Connections

You can test your WebSocket tunnel using various tools:

Browser Console

const ws = new WebSocket('wss://your-subdomain.your-domain.com');
ws.onopen = () => console.log('Connected');
ws.onmessage = (event) => console.log('Message:', event.data);
ws.send('Hello WebSocket!');

Command Line Tools

# Using wscat (install with: npm install -g wscat)
wscat -c wss://your-subdomain.your-domain.com

Common Use Cases

  • Real-time applications: Chat applications, live updates, notifications
  • Live data feeds: Stock prices, sensor data, live metrics
  • Collaborative tools: Real-time document editing, shared whiteboards
  • Gaming: Multiplayer games, real-time game state synchronization