How to maintain WebSocket client connections in Durable Object?
1) Would it be the correct understanding that in order to use the Durable Objects Hibernation API, I need to extends the DurableObject class?
2) If so, how can I delete a certain clientId on close? In the examples, it doesn't extend the DurableObject class so the handleWebsocketSession method is able to set listeners to delete clientConnectionIds. However, when extending the durableObject class, the webSocketClose and webSocketError methods handle the closing of the client, so I'm not able to refer to the same clientId anymore..
See code below:
2 Replies
When extending the DurableObject class
Previously, I was able to do...
Or fundamentally, how can I restructure this whole thing so that I'm able to broadcast to all clients but delete the clients when they disconnect? I'm able to do it when NOT extending the "DurableObject" class, but then if I don't extend it, I don't think I'm able to use the Hibernation API via
this.ctx.acceptWebsocket(server)
As per Skye, the answer is below:
You'd probably want to use the tags option when accepting the websocket found here, alongside the getTags and getWebsockets method, rather than maintaining your own state, as that won't persist across hibernations
Cloudflare Docs
WebSockets · Cloudflare Durable Objects docs
WebSockets are long-lived TCP connections that enable bi-directional, real-time communication between client and server.