Multiple Producers on same Queue
I’m not an expert on miniflare but I’ll look into how to make sure multiple local workers are bound to the same Queue.
fetch and queue entrypoints on the same script.
wrangler dev --local instances automagically bind to the same queue, but it does give you the ability to emulate a full queue workflow locally.index.js:script from the mounts to the source of your worker scripts. Here's the miniflare API for reference: https://miniflare.dev/get-started/apiqueueindex.jsnode --experimental-vm-modules index.js
MessageBatch {
queue: 'my-queue',
messages: [
Message {
body: 'hello',
id: 'my-queue-0',
timestamp: 2023-04-11T22:05:51.563Z
}
]
}
MessageBatch {
queue: 'my-queue',
messages: [
Message {
body: 'hello',
id: 'my-queue-1',
timestamp: 2023-04-11T22:05:54.659Z
}
]
}❯ curl 127.0.0.1:8787/second
ok%
❯ curl 127.0.0.1:8787/first
ok%scriptimport { Miniflare } from "miniflare";
const mf = new Miniflare({
mounts: {
first: {
// https://github.com/cloudflare/miniflare/blob/1a2cc368bfbe4d117e781ed4e74309c8da0c10d1/packages/queues/src/plugin.ts#L16-L19
queueBindings: [{ name: "MY_QUEUE", queueName: "my-queue" }],
routes: ["*/first"],
modules: true,
script: `export default {
async fetch(request, env, ctx) {
await env.MY_QUEUE.send("hello");
return new Response("ok");
}
}`,
},
second: {
// https://github.com/cloudflare/miniflare/blob/1a2cc368bfbe4d117e781ed4e74309c8da0c10d1/packages/queues/src/plugin.ts#L16-L19
queueBindings: [{ name: "MY_QUEUE", queueName: "my-queue" }],
routes: ["*/second"],
modules: true,
script: `export default {
async fetch(request, env, ctx) {
await env.MY_QUEUE.send("hello");
return new Response("ok");
}
}`,
},
consumer: {
// https://github.com/cloudflare/miniflare/blob/1a2cc368bfbe4d117e781ed4e74309c8da0c10d1/packages/queues/src/plugin.ts#L21-L27
queueConsumers: [{ queueName: "my-queue" }],
modules: true,
script: `export default {
async queue(messages, env, ctx) {
console.log(messages);
}
}`,
},
},
});
await mf.startServer();