this.ctx.getWebSockets() return array of empty objects
When I run this.ctx.getWebSockets() from my durableObject it returns me an array of empty objects, but I expected to actually receive the websockets, what am I missing?
20 Replies
Are you just
console.log
ing them?JSON.stringy them then console.log ing them
Yeah, I don't think they are stringifiable
O
They are there
How can I see their properties etc? Because I am not that familiar
Cloudflare Docs
WebSockets · Cloudflare Workers docs
Communicate in real time with your Cloudflare Workers.
You were correct! If I just assume it contains a websocket, it actually has one
Do you happen to know if the websockets have an id or a name I can use to reference them and make disctions between the websockets themselves? I don't see anything in the api
Or do I just use the websocket itself as a id? So I can do ws === ws?
Give each a unique tag
Yeah, but the problem is how do I relate a WebSocket to this unique tag
I only have the websocket itself
I want to do something like this:
async fetch(request) {
const webSocketPair = new WebSocketPair();
const [client, server] = Object.values(webSocketPair);
this.sessions.set(client, crypto.randomUUID());
}
They do the same here: https://github.com/cloudflare/workers-chat-demo/blob/master/src/chat.mjs
GitHub
workers-chat-demo/src/chat.mjs at master · cloudflare/workers-chat-...
Contribute to cloudflare/workers-chat-demo development by creating an account on GitHub.
(well similar), but for me it doesn't work
I wouldn't store the actual client anywhere, since that would defeat the purpose of the hibernation API
But how would I send a message to a specific websocket? So let's say my durable object has a connection with a,b,c, and I want to send a message from a -> c, how can I do that?
I need some way to get the correct websocket for C based on the input of A, so I need some kind of pointer
this.ctx.getWebSockets("c")
But how do I give them a tag?
How do I know incoming messager from a websocket is for example "A" or "B" or "C"
InacceptWebSocket?
this.ctx.acceptWebSocket(server, ["c"])
O, and then I can use getTags to see which websocket is currently sending
I think I finally get it
Yeah
Thanks a lot for the help, really helped me out here!