How to deploy a websocket to Cloudflare?

I want to deploy this web socket server, called y-websocket.

Normally I would start it locally via

HOST=localhost PORT=1234 npx y-websocket

but how would I do it here? Also how do I make it sure, that only my frontend, deployed to pages can connect to it?

You’re not going to be able run something like y-websocket directly with Workers as it uses a lot of Node libraries.

Workers do support WebSockets though, see the following docs:


@cherryjimbo Many thanks for getting back to me.

So when I start with this,, and I want to connect my frontend, hosted at pages, to it, how would I do it. I am still struggling how to set things up correctly, from a workers side.

@cherryjimbo Many thanks again for your input. Do you have some concrete manual/code fragments or so to make this websocket run in worker? :slight_smile:

I do not, sorry. What have you tried? What errors have you run into? I would recommend consulting the docs for y-websocket, but I doubt you’re going to be able to have it run within a Worker directly, and will need to rebuild your code around the WebSocket support in Workers, with the documentation I previously linked:

My issue is that I am totally inexperienced how to start such a thing. Maybe I have to stress that I am not a Javascript expert at all, so I can run things but modifying is hard for me. Hence, I would need some kind of template or something form where the necessary modifications are obvious.