for local dev, route DO requests to the DO fetch worker instead of the direct binding since multiple
for local dev, route DO requests to the DO fetch worker instead of the direct binding since multiple DO bindings local doesn't really work
this.state.acceptWebSocket(socket, topic), then to find connections by topic you would do this.state.getWebSockets(topic)this.storage.transactionSync(() => {...}) directly. In the callback, you can do a query, process it with code, do another query, etc. Then, every storage operation in that callback will either all-succeed or all-fail.this.state.acceptWebSocket(socket)async fetch(request) {
const upgradeHeader = request.headers.get("Upgrade");
if (upgradeHeader === "websocket") {
const [client, server] = Object.values(new WebSocketPair());
this.state.acceptWebSocket(server);
const isFirst = this.sessions.size === 0;
server.serializeAttachment({ isFirst });
this.sessions.set(server, { isFirst });
if (!isFirst) {
console.log("sessions.size: ", this.sessions.size);
// Debug
for (let [ws, data] of this.sessions.entries()) {
console.log("WS:", ws, "DATA:", data);
}
// find the connection with session.isFirst === true
const [firstWs, firstSession] = [...this.sessions.entries()].find(([_, session]) => session.isFirst) || [];
console.log("Found firstWs:", firstWs, "firstSession:", firstSession);
if (firstWs) {
console.log("Sending sync.")
firstWs.send(JSON.stringify({ Evento: "sincronizar_fila", Comando: "" }));
}
}
// When connect, send the stored musics
const storedMusics = await this.state.storage.get("musicas");
if (storedMusics) {
server.send(JSON.stringify({ Evento: "musicas", Comando: storedMusics }));
}
return new Response(null, { status: 101, webSocket: client });
}
return new Response("The request is not a websocket.", { status: 400 });
}
async webSocketClose(ws) {
let session = this.sessions.get(ws);
this.sessions.delete(ws);
if (session?.isFirst) {
console.log("The first. Closing all");
await this.state.storage.delete("musicas");
for (let [otherWs] of this.sessions.entries()) {
otherWs.close();
this.sessions.delete(otherWs);
}
}
ws.close();
}

5 Duration billing charges for the 128 MB of memory your Durable Object is allocated, regardless of actual usage. If your account creates many instances of a single Durable Object class, Durable Objects may run in the same isolate on the same physical machine and share the 128 MB of memory. These Durable Objects are still billed as if they are allocated a full 128 MB of memory.
this.state.getWebSockets()?wrangler dev in each folder? Or should i have all the DO bindings defined on a root wrangler.toml? In my case I have a sveltekit app that I'm also deploying on a worker, and was wondering if I should be defining all my DO bindings on my sveltekit wrangler.toml this.state.acceptWebSocket(socket, topic)topicthis.state.getWebSockets(topic)this.storage.transactionSync(() => {...})this.state.acceptWebSocket(socket)async fetch(request) {
const upgradeHeader = request.headers.get("Upgrade");
if (upgradeHeader === "websocket") {
const [client, server] = Object.values(new WebSocketPair());
this.state.acceptWebSocket(server);
const isFirst = this.sessions.size === 0;
server.serializeAttachment({ isFirst });
this.sessions.set(server, { isFirst });
if (!isFirst) {
console.log("sessions.size: ", this.sessions.size);
// Debug
for (let [ws, data] of this.sessions.entries()) {
console.log("WS:", ws, "DATA:", data);
}
// find the connection with session.isFirst === true
const [firstWs, firstSession] = [...this.sessions.entries()].find(([_, session]) => session.isFirst) || [];
console.log("Found firstWs:", firstWs, "firstSession:", firstSession);
if (firstWs) {
console.log("Sending sync.")
firstWs.send(JSON.stringify({ Evento: "sincronizar_fila", Comando: "" }));
}
}
// When connect, send the stored musics
const storedMusics = await this.state.storage.get("musicas");
if (storedMusics) {
server.send(JSON.stringify({ Evento: "musicas", Comando: storedMusics }));
}
return new Response(null, { status: 101, webSocket: client });
}
return new Response("The request is not a websocket.", { status: 400 });
}
async webSocketClose(ws) {
let session = this.sessions.get(ws);
this.sessions.delete(ws);
if (session?.isFirst) {
console.log("The first. Closing all");
await this.state.storage.delete("musicas");
for (let [otherWs] of this.sessions.entries()) {
otherWs.close();
this.sessions.delete(otherWs);
}
}
ws.close();
}this.state.getWebSockets()wrangler dev