Queue
Source:
src/Cloudflare/Queue/Queue.ts
A Cloudflare Queue for reliable message passing between Workers.
Queues enable you to send and receive messages with guaranteed delivery. Create a queue as a resource, then bind it to a Worker to send messages at runtime. Register a consumer to process messages.
Creating a Queue
Section titled “Creating a Queue”Basic queue
const queue = yield* Cloudflare.Queue("MyQueue");Queue with explicit name
const queue = yield* Cloudflare.Queue("MyQueue", { name: "my-app-queue",});Binding to a Worker
Section titled “Binding to a Worker”In an Effect-style Worker, use Cloudflare.QueueBinding.bind in
the init phase and provide Cloudflare.QueueBindingLive in the
runtime layer. The returned QueueSender exposes send and
sendBatch.
import * as Cloudflare from "alchemy/Cloudflare";import * as Effect from "effect/Effect";import { HttpServerRequest } from "effect/unstable/http/HttpServerRequest";import * as HttpServerResponse from "effect/unstable/http/HttpServerResponse";
export const Queue = Cloudflare.Queue("Queue");
export default Cloudflare.Worker( "Worker", { main: import.meta.path }, Effect.gen(function* () { const queue = yield* Cloudflare.QueueBinding.bind(Queue);
return { fetch: Effect.gen(function* () { const request = yield* HttpServerRequest; if (request.url === "/queue/send" && request.method === "POST") { const text = yield* request.text; yield* queue.send({ text, sentAt: Date.now() }).pipe(Effect.orDie); return yield* HttpServerResponse.json( { sent: { text } }, { status: 202 }, ); } return HttpServerResponse.text("Not Found", { status: 404 }); }), }; }).pipe(Effect.provide(Cloudflare.QueueBindingLive)),);