How do i send data from worker to durable object fetch?

Trying to do something along the lines of
const body = JSON.stringify({
chatterId: request.chatterId,
roomId: roomId,
clientId: request.clientId,
});

return await roomDurable.fetch(request, {body: body});
const body = JSON.stringify({
chatterId: request.chatterId,
roomId: roomId,
clientId: request.clientId,
});

return await roomDurable.fetch(request, {body: body});
but I keep getting 500 Internal Server Error when i try this. If i remove the "body" part from the fetch, it all works. I need to pass in data like this but I've been having trouble finding examples of this. Also the request is just the request I get from a worker, should that be there?
1 Reply
TheKaiser
TheKaiserOP15mo ago
I'm pretty sure i need it, if i get rid of the {body: body} and make it just body my editor flags it because it needs to be a 'RequestInit<CfProperties<unknown>>' I'm not getting any usefull error messages, just "500 Internal Server Error" when running wrangler dev locally and
/home/kai/Code/ez-chat/node_modules/wrangler/wrangler-dist/cli.js:30632
throw a;
^

Error: read ECONNRESET
at TCP.onStreamRead (node:internal/stream_base_commons:217:20)
Emitted 'error' event on Socket instance at:
at emitErrorNT (node:internal/streams/destroy:151:8)
at emitErrorCloseNT (node:internal/streams/destroy:116:3)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
errno: -104,
code: 'ECONNRESET',
syscall: 'read'
}

Node.js v20.5.1
/home/kai/Code/ez-chat/node_modules/wrangler/wrangler-dist/cli.js:30632
throw a;
^

Error: read ECONNRESET
at TCP.onStreamRead (node:internal/stream_base_commons:217:20)
Emitted 'error' event on Socket instance at:
at emitErrorNT (node:internal/streams/destroy:151:8)
at emitErrorCloseNT (node:internal/streams/destroy:116:3)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
errno: -104,
code: 'ECONNRESET',
syscall: 'read'
}

Node.js v20.5.1
when running wrangler dev remotely Figured it out! I wrapped the fetch in a ctx.waitUntil() and for some reason that finally told me the error was because I was including a body in a GET http call. The line now just looks like
return await roomDurable.fetch(request, { method: "POST", body: body });
return await roomDurable.fetch(request, { method: "POST", body: body });
even though it doesn't really matter what the method is, the DO is really simple and only has one route also in case anyone else is trying this I needed to include an application type header so the full line looks like this now
return await roomDurable.fetch(request, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
return await roomDurable.fetch(request, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
Ok final time posting here, I have now spent the last 48 hours trying to get this to work and it turns out things don't work well since the "upgrade": "websocket" header seems to only work with GET calls, and GET calls don't support bodys. The final solution I've come to is copying what the "itty-durable" library does and pass in the data through the header. my fetch now looks like this
const ret = await roomDurable.fetch(request, {
headers: { ...request.headers.values(), "do-content": JSON.stringify(content), Upgrade: "websocket" },
});
const ret = await roomDurable.fetch(request, {
headers: { ...request.headers.values(), "do-content": JSON.stringify(content), Upgrade: "websocket" },
});
I'm posting this in the small chance someone else comes across the same issue. TBH this experience has soured me a little bit on durable objects and the cloudflare worker environment but whatever its too late for me to switch now.
Want results from more Discord servers?
Add your server