Reverse proxy to websocket not working

Hi,

I’m trying to use a Cloudflare worker as a Reverse proxy for https and websocket requests, but can’t get web socket proxing to work at all. I just get an error saying Fetch API cannot load: wss://<origin web socket server URL>. I’ve tried pointing to a few different websocket servers and always get the same error - suggesting that it’s something within my cloudflare setup that’s wrong. My worker code is based on loads of standard examples out there - I don’t think that’s the problem.

Any ideas what I’m doing wrong?

Thanks in advance.

Cheers,
Ian

1 Like

This is the way it works:

// Worker code
export default {
  async fetch(request, env, ctx) {
    return fetch('https://ws.postman-echo.com/raw', request);
  },
};
// Client code
const ws = new WebSocket('wss://your-worker-url.workers.dev');
// ...

You need to use the https: protocol when overriding the connection URL, and Cloudflare will do the rest under the hood.

Read more here.