> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.scaleabm.org/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.scaleabm.org/_mcp/server.

# WebSocket Relay

> Route LinkedIn API calls through your real Chrome browser for maximum session fidelity

# WebSocket Relay

The WebSocket relay lets the Voyager server dispatch LinkedIn API calls through the user's real Chrome browser instead of issuing them from the server. The Chrome extension holds a WebSocket to the server, receives requests, executes them with `fetch()` and `credentials: 'include'`, and streams the response back. LinkedIn sees requests coming from the user's actual browser with real cookies and a real residential IP.

## Why use it

* **Session fidelity.** Requests carry the user's real browser cookies, User-Agent, TLS fingerprint, and IP. No datacenter-IP tell, no fingerprint mismatch.
* **Zero LinkedIn tabs needed.** The extension's service worker handles fetches directly — users don't have to keep a LinkedIn tab open.
* **Self-healing cookies.** When the server detects a stale session, it sends a `cookie_sync_request`; the extension grabs fresh cookies from Chrome's cookie store and syncs them back automatically.
* **Graceful fallback.** When the relay isn't available (Chrome closed, extension uninstalled, WebSocket drop), every endpoint falls back to direct HTTP + residential proxy using the stored cookies — just regular HTTPS. There is no third tier; Puppeteer was removed 2026-04-22.
* **Authwall detection.** If the extension observes a redirect to a LinkedIn authwall or checkpoint page while loading content, it alerts the server immediately.

## How it works

```
Chrome Extension (service worker)
    │  WebSocket (wss://)
    ▼
Voyager Server (/api/relay)
    │
    │  Routes an outbound LinkedIn call to the relay
    │  (if the connection is healthy)
    ▼
Extension executes: fetch(url, { credentials: 'include' })
    │  Chrome attaches real cookies, uses real IP
    ▼
LinkedIn API
```

1. The Chrome extension connects to `wss://<server>/api/relay` on startup.
2. It sends an `auth` message with the API key and user ID.
3. The server validates the key and registers the connection.
4. When Voyager needs to call LinkedIn, it picks the relay if available, or falls back to direct HTTP + residential proxy.
5. The extension executes the request, Chrome attaches real cookies, the response comes back through the WebSocket.
6. Voyager returns the result to the endpoint caller.

> **Tip**
>
> The relay is transparent to all Voyager endpoints. Whether a request goes through the relay or direct HTTP, the API response format is identical.

## Protocol

### Authentication

The first message on the WebSocket must be an `auth` message:

```json
{ "type": "auth", "apiKey": "voy_xxx", "userId": "charis" }
```

The server responds:

```json
{ "type": "authenticated", "tenantId": "32e365dc", "userId": "charis" }
```

If authentication fails, or no auth message arrives within 10 seconds, the server closes the connection.

### Request / Response

The server sends request messages:

```json
{
  "type": "request",
  "id": "r1_m3k9f",
  "method": "GET",
  "url": "https://www.linkedin.com/voyager/api/identity/profiles/...",
  "headers": { "Accept": "application/json" },
  "body": null
}
```

The extension replies with the same `id`:

```json
{
  "type": "response",
  "id": "r1_m3k9f",
  "status": 200,
  "body": "{...}"
}
```

Or on failure:

```json
{ "type": "error", "id": "r1_m3k9f", "error": "net::ERR_NETWORK_CHANGED" }
```

### Ping / Pong Keepalive

The server pings every 20 seconds to keep the MV3 service worker alive:

```json
{ "type": "ping" }
```

The extension replies with `{ "type": "pong" }`.

> **Note**
>
> MV3 service workers are killed after 30s of inactivity. The 20s ping keeps the worker warm. As a backup, the extension also maintains a Chrome alarm (`relay-keepalive`) that fires every 24s — if the worker was killed and restarted, it reconnects automatically.

## Auto-Reconnect

If the WebSocket disconnects (server redeploy, network change, Chrome sleep, etc.), the extension reconnects with exponential backoff:

| Attempt | Delay             |
| ------- | ----------------- |
| 1st     | 1s                |
| 2nd     | 2s                |
| 3rd     | 4s                |
| 4th     | 8s                |
| …       | doubles each time |
| Max     | 60s               |

The backoff resets to 1s on successful connection. When the user changes the API key, user ID, or server URL in options, the extension closes and immediately reconnects.

> **Note**
>
> Voyager's retry layer is explicitly aware of relay drops. An outbound call that fails with `"Relay connection closed"` is classified as a retryable transport error — the next attempt falls through to direct HTTP + proxy, so a momentary relay drop rarely surfaces to the caller as a failure.

## Extension Badge

The Chrome extension icon reflects the relay status:

| Badge    | Colour            | Meaning                                                                         |
| -------- | ----------------- | ------------------------------------------------------------------------------- |
| **R**    | Blue (`#2563eb`)  | Relay active — requests route through Chrome                                    |
| **!**    | Red (`#dc2626`)   | Server unreachable, or LinkedIn session invalid                                 |
| *(none)* | Green (`#059669`) | Connected to the server; traffic will use direct HTTP until Chrome is available |

## Status Endpoints

### Check relay status (user-scoped)

```bash
curl -H "Authorization: Bearer $KEY" -H "X-User-Id: $USER" \
  "$BASE/api/relay/check"
```

```json
{ "relayConnected": true, "fetcherSet": true }
```

* `relayConnected` — WebSocket is open for this user
* `fetcherSet` — the relay fetcher is wired into `VoyagerApi` and `SalesNavApi`

### Admin relay status

```bash
curl -H "Authorization: Bearer $ADMIN_SECRET" \
  "$BASE/api/admin/relay/status"
```

```json
{
  "connections": [
    {
      "tenantId": "32e365dc",
      "userId": "charis",
      "connectedAt": 1742601234567,
      "requestCount": 47
    }
  ]
}
```

## Fallback Behaviour

When the relay isn't usable, Voyager falls back to direct HTTP + residential proxy — not to a headless browser.

| Scenario                           | Behaviour                                                                                               |
| ---------------------------------- | ------------------------------------------------------------------------------------------------------- |
| Chrome closed / extension disabled | Tier 2 (direct HTTP + IPRoyal proxy) handles every request                                              |
| WebSocket drops mid-request        | The in-flight request fails with `"Relay connection closed"`; the retry attempt falls through to Tier 2 |
| Chrome reopens                     | Extension reconnects within \~1s; subsequent calls use the relay again                                  |
| Server redeploys                   | WebSocket drops, extension reconnects with backoff once the new instance is up                          |

> **Warning**
>
> SalesNav endpoints that fingerprint a real Chrome session are more robust through the relay, but most still work on Tier 2 as well. If SalesNav specifically fails on direct HTTP for a tenant, check whether `li_a` and `li_ep_auth_context` are synced.

## Configuration

The server side needs no configuration — the relay is always available at `/api/relay`. The extension needs:

1. **API key** — set in the extension options page
2. **User ID** — set in the extension options page
3. **Server URL** — defaults to `https://li.scaleabm.org`, configurable in options

The extension connects the relay automatically on startup. No manual activation is needed.