Gary Somerhalder
05/22/2023, 5:44 PMDani Foldi
05/22/2023, 5:45 PMGary Somerhalder
05/22/2023, 5:45 PMGary Somerhalder
05/22/2023, 5:45 PMSkye
05/22/2023, 5:45 PMGary Somerhalder
05/22/2023, 5:46 PMGary Somerhalder
05/22/2023, 5:46 PMGary Somerhalder
05/22/2023, 5:47 PMRehman Afzal
05/23/2023, 4:53 AMmizchi
05/23/2023, 2:27 PMts
import { Env } from "./types";
// websocket handler durable objects
export { TestObject } from "./test_object";
export default {
async fetch(
request: Request,
env: Env,
_ctx: ExecutionContext
): Promise<Response> {
return await handleErrorsWithUgrade(request, async () => {
if (request.url.endsWith("/connect")) {
const id = env.test.idFromName("test");
const testObject = env.test.get(id);
return testObject.fetch(request);
}
return Response.json({ ok: true });
});
},
async queue(batch: MessageBatch<Error>, env: Env): Promise<void> {
let messages = JSON.stringify(batch.messages);
const id = env.test.idFromName("test");
// Here: I want to connect existed socket and emit(log)
// const testObject = env.test.get(id);
// testObject.connect() ????
console.log(`consumed from our queue: ${messages}`);
},
};
async function handleErrorsWithUgrade(request: Request, func: any) {
try {
return await func();
} catch (err) {
if (request.headers.get("Upgrade") === "websocket") {
const pair = new WebSocketPair();
pair[1].accept();
// @ts-ignore
pair[1].send(JSON.stringify({ error: err.stack }));
pair[1].close(1011, "Uncaught exception during session setup");
return new Response(null, { status: 101, webSocket: pair[0] });
}
// @ts-ignore
return new Response(err.stack, { status: 500 });
}
}
Yaron
05/23/2023, 9:20 PMconst queueTweet = async (tweetId: string, prompt: string, env: any) => {
const log = {tweetId, prompt};
await env.TWITTER_MENTIONS_QUEUE.send(log);
console.log('added to queue', log) // Console log is fired successfully
}
And a consumer like this
[[queues.consumers]]
queue = "twitter-mentions-queue"
max_batch_size = 10
max_batch_timeout = 5
async queue(batch: MessageBatch<Error>, env: Environment): Promise<void> {
for (const message of batch.messages) {
console.log('Received', message); // This never runs, looked for it in tail, also the ui shows zero queue messages always
}
},
Any ideas why I can't see messages?emilio
05/23/2023, 9:29 PMbatch
to just MessageBatch
instead of MessageBatch<Error>
Yaron
05/23/2023, 10:53 PMmizchi
05/24/2023, 5:06 AMrohanbabuam
05/24/2023, 6:30 AMelithrar
05/24/2023, 12:30 PMrohanbabuam
05/24/2023, 2:39 PMLarry
05/24/2023, 10:00 PMconst testObject
is scoped to just the worker fetch handler. You can't even see it inside your queue handler. The second problem, that I suspect you will have is that even if you declare testObject before the start of the main worker fetch() handler so it's in scope, you have no guarantee that the worker instance that created the connection will be the same one as the one that's called by cloudflare via your queue() handler. It's best to think of these two pieces of code as completely seperate. Cloudflare will grabe them and instantiate them as needed. Once they are done running, they can disappear.
If you really have a good reason to use WebSockets from your queue() handler, you'll have to create the connection to the DO from inside there, but the only reason I can think of for doing that is that it might be slightly more efficient to make multiple round trips to a DO this way. For a single call though, the traditional way of talking to a DO is going to be more efficient because you don't have the handshake overhead of establishing the WebSocket connection. That said, it would be a premature optimization to even try this.mizchi
05/25/2023, 2:07 PMmizchi
05/25/2023, 2:13 PMmizchi
05/25/2023, 2:16 PMlvmcg
05/26/2023, 9:50 AMrodbs
05/26/2023, 9:59 PMwaituntil
and it fails. Not user the reason; it might because a Worker is not capable of handle it ...
https://discord.com/channels/595317990191398933/1100814451151741028/1101567116890620015
My question is: would it work with a Queue? or because underlying it'' s executing a Worker I'll experience the same issues?kian
05/26/2023, 10:01 PMwaitUntil
wouldn't be suitablekian
05/26/2023, 10:01 PMrodbs
05/26/2023, 10:02 PMVâjhâtz
12/18/2023, 3:27 PMtreedor
12/24/2023, 1:59 PMdelay
feature will be out sometime soon? Similar timeline as HTTP pull?