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
Milan
Milan4w ago
@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
lukejacksonn
lukejacksonnOP4w ago
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:
session.setWebSocketAutoResponse({
request: "ping",
response: "pong"
})
session.setWebSocketAutoResponse({
request: "ping",
response: "pong"
})
which I thought I saw somewhere.. but it throws:
Uncaught (in response) TypeError: Failed to execute 'setWebSocketAutoResponse' on 'DurableObjectState': parameter 1 is not of type 'WebSocketRequestResponsePair'.
Uncaught (in response) TypeError: Failed to execute 'setWebSocketAutoResponse' on 'DurableObjectState': parameter 1 is not of type 'WebSocketRequestResponsePair'.
Milan
Milan4w ago
Brb in like an hour or so
lukejacksonn
lukejacksonnOP4w ago
Ok no worries!
Milan
Milan4w ago
sorry got bogged down by stuff. This should work
this.ctx.setWebSocketAutoResponse(new WebSocketRequestResponsePair("foo", "bar"))
this.ctx.setWebSocketAutoResponse(new WebSocketRequestResponsePair("foo", "bar"))
Milan
Milan4w ago
GitHub
workerd/src/workerd/api/actor-state.h at efb5ff9a9132b2d40489ddb5a7...
The JavaScript / Wasm runtime that powers Cloudflare Workers - cloudflare/workerd
lukejacksonn
lukejacksonnOP4w ago
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?
Milan
Milan4w ago
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"
lukejacksonn
lukejacksonnOP4w ago
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:
this.state.setWebSocketAutoResponse(
new WebSocketRequestResponsePair("ping", "pong")
);
this.state.setWebSocketAutoResponse(
new WebSocketRequestResponsePair("ping", "pong")
);
Then on the client (the browser in this case):
ws.send("ping")
ws.send("ping")
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 🙇‍♂️
Milan
Milan4w ago
No worries, happy to help 🙂, good luck building!
Want results from more Discord servers?
Add your server