|
| 1 | +--- |
| 2 | +title: Publish to a Queue via Workers |
| 3 | +summary: Publish to a Queue directly from your Worker. |
| 4 | +pcx_content_type: example |
| 5 | +sidebar: |
| 6 | + order: 30 |
| 7 | +head: |
| 8 | + - tag: title |
| 9 | + content: Queues - Publish Directly via a Worker |
| 10 | +description: Publish to a Queue directly from your Worker. |
| 11 | +--- |
| 12 | + |
| 13 | +import { WranglerConfig } from "~/components"; |
| 14 | + |
| 15 | +The following example shows you how to publish messages to a Queue from a Worker. The example uses a Worker that receives a JSON payload from the request body and writes it as-is to the Queue, but in a real application you might have more logic before you queue a message. |
| 16 | + |
| 17 | +## Prerequisites |
| 18 | + |
| 19 | +- A [queue created](/queues/get-started/#3-create-a-queue) via the [Cloudflare dashboard](https://dash.cloudflare.com) or the [wrangler CLI](/workers/wrangler/install-and-update/). |
| 20 | +- A [configured **producer** binding](/queues/configuration/configure-queues/#producer-worker-configuration) in the Cloudflare dashboard or Wrangler file. |
| 21 | + |
| 22 | +Configure your Wrangler file as follows: |
| 23 | + |
| 24 | +<WranglerConfig> |
| 25 | + |
| 26 | +```toml |
| 27 | +name = "my-worker" |
| 28 | + |
| 29 | +[[queues.producers]] |
| 30 | + queue = "my-queue" |
| 31 | + binding = "YOUR_QUEUE" |
| 32 | + |
| 33 | +``` |
| 34 | + |
| 35 | +</WranglerConfig> |
| 36 | + |
| 37 | +### 1. Create the Worker |
| 38 | + |
| 39 | +The following Worker script: |
| 40 | + |
| 41 | +1. Validates that the request body is valid JSON. |
| 42 | +2. Publishes the payload to the queue. |
| 43 | + |
| 44 | +```ts |
| 45 | +interface Env { |
| 46 | + YOUR_QUEUE: Queue; |
| 47 | +} |
| 48 | + |
| 49 | +export default { |
| 50 | + async fetch(req, env): Promise<Response> { |
| 51 | + // Validate the payload is JSON |
| 52 | + // In a production application, we may more robustly validate the payload |
| 53 | + // against a schema using a library like 'zod' |
| 54 | + let messages; |
| 55 | + try { |
| 56 | + messages = await req.json(); |
| 57 | + } catch (e) { |
| 58 | + // Return a HTTP 400 (Bad Request) if the payload isn't JSON |
| 59 | + return Response.json({ err: "payload not valid JSON" }, { status: 400 }); |
| 60 | + } |
| 61 | + |
| 62 | + // Publish to the Queue |
| 63 | + try { |
| 64 | + await env.YOUR_QUEUE.send(messages); |
| 65 | + } catch (e: any) { |
| 66 | + console.log(`failed to send to the queue: ${e}`); |
| 67 | + // Return a HTTP 500 (Internal Error) if our publish operation fails |
| 68 | + return Response.json({ error: e.message }, { status: 500 }); |
| 69 | + } |
| 70 | + |
| 71 | + // Return a HTTP 200 if the send succeeded! |
| 72 | + return Response.json({ success: true }); |
| 73 | + }, |
| 74 | +} satisfies ExportedHandler<Env>; |
| 75 | +``` |
| 76 | + |
| 77 | +To deploy this Worker: |
| 78 | + |
| 79 | +```sh |
| 80 | +npx wrangler deploy |
| 81 | +``` |
| 82 | + |
| 83 | +### 2. Send a test message |
| 84 | + |
| 85 | +To make sure you successfully write a message to your queue, use `curl` on the command line: |
| 86 | + |
| 87 | +```sh |
| 88 | +# Make sure to replace the placeholder with your shared secret |
| 89 | +curl -XPOST "https://YOUR_WORKER.YOUR_ACCOUNT.workers.dev" --data '{"messages": [{"msg":"hello world"}]}' |
| 90 | +``` |
| 91 | + |
| 92 | +```sh output |
| 93 | +{"success":true} |
| 94 | +``` |
| 95 | + |
| 96 | +This will issue a HTTP POST request, and if successful, return a HTTP 200 with a `success: true` response body. |
| 97 | + |
| 98 | +- If you receive a HTTP 400, this is because you attempted to send malformed JSON to your queue. |
| 99 | +- If you receive a HTTP 500, this is because the message was not written to your Queue successfully. |
| 100 | + |
| 101 | +You can use [`wrangler tail`](/workers/observability/logs/real-time-logs/) to debug the output of `console.log`. |
0 commit comments