Skip to content

Commit e91b657

Browse files
authored
[Queues] Add a changelog entry and examples for the new HTTP Push API (#22315)
* [Queues] Simplify example that pushes to a Queue via a Worker * [Queues] Add an example to demo HTTP Push * [Queues] Update CODEOWNERS
1 parent a45e666 commit e91b657

File tree

5 files changed

+144
-167
lines changed

5 files changed

+144
-167
lines changed

.github/CODEOWNERS

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@
116116
/src/content/release-notes/kv.yaml @elithrar @thomasgauvin @rts-rob @oxyjun @cloudflare/pcx-technical-writing
117117
/src/content/partials/kv/ @elithrar @thomasgauvin @rts-rob @oxyjun @cloudflare/pcx-technical-writing
118118
/src/content/docs/pub-sub/ @elithrar @dcpena @cloudflare/pcx-technical-writing
119-
/src/content/docs/queues/ @elithrar @maheshwarip @harshil1712 @cloudflare/pcx-technical-writing
120-
/src/content/release-notes/queues.yaml @elithrar @maheshwarip @cloudflare/pcx-technical-writing
119+
/src/content/docs/queues/ @elithrar @jonesphillip @harshil1712 @cloudflare/pcx-technical-writing
120+
/src/content/release-notes/queues.yaml @elithrar @jonesphillip @cloudflare/pcx-technical-writing
121121
/src/content/docs/r2/ @oxyjun @elithrar @jonesphillip @aninibread @harshil1712 @cloudflare/workers-docs @cloudflare/pcx-technical-writing
122122
/src/content/release-notes/r2.yaml @oxyjun @elithrar @aninibread @cloudflare/workers-docs @cloudflare/pcx-technical-writing
123123
/src/content/docs/realtime/ @cloudflare/pcx-technical-writing @cloudflare/calls

public/__redirects

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,7 @@
11711171
/queues/reference/consumer-concurrency/ /queues/configuration/consumer-concurrency/ 301
11721172
/queues/reference/local-development/ /queues/configuration/local-development/ 301
11731173
/queues/reference/metrics/ /queues/observability/metrics/ 301
1174+
/queues/examples/publish-to-a-queue-over-http/ /queues/examples/publish-to-a-queue-via-http/ 301
11741175

11751176

11761177
# reference architectures & design guides

src/content/docs/queues/examples/publish-to-a-queue-over-http.mdx

Lines changed: 0 additions & 165 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
title: Publish to a Queue via HTTP
3+
summary: Publish to a Queue directly via HTTP.
4+
pcx_content_type: example
5+
sidebar:
6+
order: 31
7+
head:
8+
- tag: title
9+
content: Queues - Publish Directly via HTTP
10+
description: Publish to a Queue directly via HTTP and Workers.
11+
---
12+
13+
The following example shows you how to publish messages to a Queue from any HTTP client, using a Cloudflare API token to authenticate.
14+
15+
This allows you to write to a Queue from any service or programming language that supports HTTP, including Go, Rust, Python or even a Bash script.
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 Cloudflare API token with the `Queues Edit` permission.
21+
22+
23+
### 1. Send a test message
24+
25+
To make sure you successfully authenticate and write a message to your queue, use `curl` on the command line:
26+
27+
```sh
28+
# Make sure to replace the placeholder with your shared secret
29+
curl -XPOST -H "Authorization: Bearer <paste-your-api-token-here>" "https://api.cloudflare.com/client/v4/accounts/<paste-your-account-id-here>/queues/<paste-your-queue-id-here>/messages" --data '{ "body": { "greeting": "hello" } }'
30+
```
31+
32+
```sh output
33+
{"success":true}
34+
```
35+
36+
This will issue a HTTP POST request, and if successful, return a HTTP 200 with a `success: true` response body.
37+
38+
- If you receive a HTTP 403, this is because your API token is invalid or does not have the `Queues Edit` permission.
39+
40+
For full documentation about the HTTP Push API, refer to the [Cloudflare API documentation](https://developers.cloudflare.com/api/resources/queues/subresources/messages/).
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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

Comments
 (0)