Hey 👋 I have a DO based off of this
Hey 👋 I have a DO based off of this https://github.com/cloudflare/workers-chat-demo and its working great.. but client connected sockets timeout (close with code
1006
) if no messages are sent after exactly 3 minutes.. is this expected?
I have tried calling state.setWebSocketAutoResponse
in the DO constructor (not sure how to use this method) but that didn't seem to do much. Do I need to have connected clients send a ping messages to the DO at a regular interval to keep the connection open? If so, does this wake the socket from hibernation (and thus incur a charge)?10 Replies
@lukejacksonn setting the auto-response gets you 50% of the way there. You then want your clients to periodically send a websocket message with the "Request" text you set.
The auto-response API basically means: if my DO receives this message, then the system will reply with the pre-defined response without invoking your application logic. Therefore, if your DO is hibernating and it receives a message it would auto-respond to, we will not wake your DO from hibernation.
IIRC our runtime (workers generally) doesn't do any keep-alive stuff for WS, but other Cloudflare services that sit between your client and your DO do enforce keep-alives, so auto-response is a great way to work around that
Thanks for the answer! Ok so I think I understood that.. do you have an example of the syntax to use for the
setWebSocketAutoResponse
call and the message that the clinet needs to send?
I tried:
which I thought I saw somewhere.. but it throws:
Brb in like an hour or so
Ok no worries!
sorry got bogged down by stuff.
This should work
The constructor is actually defined here if you're interested https://github.com/cloudflare/workerd/blob/efb5ff9a9132b2d40489ddb5a705f7c3fb189b0a/src/workerd/api/actor-state.h#L403-L405
GitHub
workerd/src/workerd/api/actor-state.h at efb5ff9a9132b2d40489ddb5a7...
The JavaScript / Wasm runtime that powers Cloudflare Workers - cloudflare/workerd
Oh great, thank you so much for taking the time! I will try that out
So that resolved the type arror and the code now runs on the server ✅ but (and excuse my ignorance here) but how do I send a
foo
request from the client?If you set
new WebSocketRequestResponsePair("foo", "bar")
, then have your client (browser, python srcipt, whatever it happens to be) do a ws.send("foo")
, easy as that 🙂
these are "application-level messages", the websocket protocol supports sending protocol level pings (which are invisible to you if you're programming a browser client for example). That's not particularly useful for most people, which is why we let you define an "application-level ping"Ahh silly me, I was doing
ws.send("foo")
from the client.. but to the worker I had yet to set the auto response on 🤦♂️
That seems to work now.. it is pinging and ponging without me having to handle those messages explicitly in the webSocketMessage
handler
For future folks with this issue..
Add this code to your DO constructor:
Then on the client (the browser in this case):
You should get back a pong
message but the DO webSocketMessage
will not be called in response to pings (which I assume confrims that the application is handling that request rather than waking up the socket from hibernation).
Forgot to say thanks for all your assistance here @Milan 🙇♂️No worries, happy to help 🙂, good luck building!