Skip to content
Browse the docs
guides

WebSockets

Tunnel WebSocket connections: dev-server hot reload, realtime apps, and any Upgrade-based protocol

WebSocket connections tunnel through zgrok the same way ordinary HTTP requests do. There is no flag to set and no separate mode: point a tunnel at a port that speaks WebSocket and the upgrade goes through.

This works on every plan, and it works with the CLI you already have: the support is server-side, so you do not need to update zgrok to get it.

Quick start

Start a tunnel on the port your WebSocket server listens on:

bash
zgrok http 3000

Then connect to the tunnel URL with the wss:// scheme, not ws://:

bash
wss://a1b2c3--acme-shop.zgrok.io

This is the single most common mistake. The plain-HTTP endpoint answers a 308 redirect to HTTPS, and WebSocket clients generally will not follow a redirect into an upgrade, so a ws:// URL fails without ever reaching your server. If your client builds its URL from window.location, derive the scheme instead of hardcoding it:

js
const scheme = location.protocol === "https:" ? "wss:" : "ws:";
const socket = new WebSocket(`${scheme}//${location.host}/socket`);

Verify the handshake

To confirm an upgrade is really going through, connect with websocat in verbose mode and read the response headers:

$ websocat -t --protocol vite-hmr -1 -v "wss://a1b2c3--acme-shop.zgrok.io/"
 
Connected to ws, response headers: Headers {
Connection: Upgrade
, Sec-WebSocket-Accept: tflfp8vwcx1JBuf/hUdJ0OdHKw4=
, Sec-WebSocket-Protocol: vite-hmr
, Upgrade: websocket
, Server: Caddy
}
{"type":"connected"}

Connection: Upgrade plus a Sec-WebSocket-Accept value means the handshake completed. The browser devtools equivalent is a 101 Switching Protocols status. Anything arriving after that (here, a JSON frame from the server) confirms real data is flowing, not just a handshake.

The --protocol vite-hmr flag matters when the server behind the tunnel is a Vite dev server: Vite only upgrades connections that ask for that subprotocol, and answers a plain 200 otherwise. Against any other WebSocket server, drop the flag.

If you get a 200 instead, your local server declined the upgrade. Check that it handles WebSocket on the exact path your client requested.

Dev server hot reload

Vite, Next.js and most modern dev servers push hot updates over a WebSocket, so sharing a dev server over a tunnel keeps hot reload working: edit a file locally and the page updates for whoever is looking at the tunnel URL.

Vite needs one config change first. Vite 7 checks the Host header of every request and rejects anything it does not recognise, so a tunneled request never reaches your app:

HTTP/2 403
 
Blocked request. This host ("a1b2c3--acme-shop.zgrok.io") is not allowed.
To allow this host, add "a1b2c3--acme-shop.zgrok.io" to `server.allowedHosts` in vite.config.js.

The 403 looks like a tunnel problem but is produced entirely by Vite. Add the host to server.allowedHosts in your vite.config.ts:

ts
// vite.config.ts
export default defineConfig({
  server: {
    allowedHosts: [".zgrok.io"],
  },
});

Use the leading-dot form .zgrok.io rather than the literal host. It matches every tunnel in every one of your projects. On Free, a tunnel without a custom name gets a new random name every time it starts, and a hardcoded host would break on the next run.

There is no CLI flag for this: Vite 7's CLI rejects --allowedHosts as an unknown option, so the config file is the only way to set it. Restart the dev server after editing.

With that in place, saving a file refetches just the changed module and re-renders it in place; the full page is never reloaded.

How it works

zgrok's edge normally rewrites a few response headers on the way out. A 101 Switching Protocols response is the exception: its head is passed through untouched, so Connection: Upgrade, Upgrade: websocket and Sec-WebSocket-Accept reach your client exactly as your server wrote them. After the handshake, frames are relayed in both directions over the same connection.

The passthrough keys off the response your server actually sends, not off the request. A server that declines an upgrade and answers 200 takes the normal path, so nothing about ordinary HTTP behaviour changes.

An idle WebSocket stays open. zgrok sends a QUIC keep-alive every few seconds against a five-minute idle timeout, so a socket that sits quiet (waiting on a notification, say) is not dropped for being quiet.

How upgrades appear in the request log

An upgrade is logged as 101 rather than 200, but the entry is written when the connection closes, not when it opens:

[16:50:51] GET / 101 144ms

A long-lived WebSocket therefore does not appear in the request log at all while it is open. If you are watching the log and see nothing, that is expected: the line arrives on disconnect, and the duration covers the whole lifetime of the connection.

Known limits

  • Custom response headers are not applied to the upgrade. Headers you add with --header are injected into normal responses, but a 101 is passed through verbatim, so nothing is added to it. Any header your client needs at handshake time must come from your own server.
  • The request log shows the handshake, not the traffic. You get one line per connection, never one per message, so individual WebSocket frames are not captured or replayable. Use your application's own logging for message-level detail.

Troubleshooting

  • The connection fails before the handshake. Check the scheme: it must be wss://, not ws://. The plain-HTTP endpoint answers a redirect rather than an upgrade, and a browser's WebSocket handshake is specified to treat a redirect as an error rather than follow it.
  • You get a 403 with "this host is not allowed". That is your dev server, not the tunnel. See the allowedHosts step above.
  • You get a 200 instead of an upgrade. Your local server did not accept it. Confirm it handles WebSocket on the exact path your client is requesting.
  • The tunnel shows a different app, or a 404 you cannot reproduce locally. Another process is listening on the same port. A curl localhost:<port> check will not necessarily reveal this: localhost resolves to either 127.0.0.1 or ::1, and two processes can hold the same port on different loopback families, so your check and the tunnel can reach different applications. List every listener with lsof -nP -iTCP:3000 -sTCP:LISTEN and start your server on an explicit host and port.
  • The connection drops after a period of silence. zgrok keeps idle tunnels warm, so this usually points at an idle timeout in your own server or a proxy in front of it.

What's next?