hi folks,
hi folks,
i'm leveraging DO for a websocket w/ hubernation support, but keen to understand how i can pass on context to my "webSocketMessage". i want to simply pass on the user id of the user into the webSocketMessage handler. when it gets hibernated, it seems to get lost. i'll drop my fairly basic code in a thread.
4 Replies
any push in the right direction would be appreciated!
Hey, you can store an attachment on each connection (up to 2048 bytes): https://developers.cloudflare.com/durable-objects/best-practices/websockets/#extended-methods
which you can serialize and deserialize any time, and these get saved with the hibernated ws, and you also have the tags which you can set when you create the connection and then retrieve later using getTags
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...
it depends on your use case, which one I'd use, if you need to get the connections (eg to forward messages) by user id, you could add a tag
userid:<actual id>
to the websocket when they connect, but if you only find out later, you can add it to the attachment (although you can't use it later for listing that efficiently)oh nice, thank you.. did not see serialize/deserialize