Websocket Server with Durable Object Client Connection
Hi, I try build a websocket server with durable object hibernation api, on the server side I think its considered done,
but on the client side, how we connect to the durable object using service binding ?
I'm using cloudflare pages and want to connect to the websocket server using service binding
anyone have code snippet to get address
already connected on the wrangler side,
but I'm strugle to get the websocket service endpoint dynamically from service binding
any advise ?
7 Replies
have handler to return
can be curl-ed
but on pages side
seem not working.

Cloudflare Docs
Using WebSockets · Cloudflare Durable Objects docs
WebSockets are long-lived TCP connections that enable bi-directional, real-time communication between client and server. Both Cloudflare Durable Objects and Workers can act as WebSocket endpoints – either as a client or as a server. Because WebSocket sessions are long-lived, applications commonly use Durable Objects to accept either the client o...
Basic code example:
- Worker handling the request (Edit: this will be your pages function):
- Websocket DO:
No faffing around with hostnames
Worker handling the request (Edit: this will be your pages function):actually I need it on the pages itself / client side. not the page function
Websocket DO:yeah already do like this. The things is we have a CF Worker - Websocket DO I need my CF Pages / any other client / agent there to connect to websocket server. on the Pages side, since there is service binding, we don't need to connect outside of cloudflare network. so when end user open my pages, they can do websocket connection to the web server and reach / communicate with others.
It is client-side, you're telling the client to switch to the websocket protocol by sending that response from the DO. Once connected, any messages that the client sends over the websocket connection will call the DO's
onMessage
handler
You need the initial request to go through the worker/pages function in order to initialise the connection. Your /init
endpoint could be used for this so that connecting clients can connect to api.yourdomain.tld/init
for examplebasically our CF Pages will act as a Proxy to our DOWebsocket ya?
got this error before
does it guaranted it will work on the prod itself?
No, a pages function - which is just a worker - should not be used as the client connecting to the DO with websockets. This should be your client - the browser