-
Notifications
You must be signed in to change notification settings - Fork 10k
[Queues] Add a changelog entry and examples for the new HTTP Push API #22315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 0 additions & 165 deletions
165
src/content/docs/queues/examples/publish-to-a-queue-over-http.mdx
This file was deleted.
Oops, something went wrong.
40 changes: 40 additions & 0 deletions
40
src/content/docs/queues/examples/publish-to-a-queue-via-http.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| --- | ||
| title: Publish to a Queue via HTTP | ||
| summary: Publish to a Queue directly via HTTP. | ||
| pcx_content_type: example | ||
| sidebar: | ||
| order: 31 | ||
| head: | ||
| - tag: title | ||
| content: Queues - Publish Directly via HTTP | ||
| description: Publish to a Queue directly via HTTP and Workers. | ||
| --- | ||
|
|
||
| The following example shows you how to publish messages to a Queue from any HTTP client, using a Cloudflare API token to authenticate. | ||
|
|
||
| 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. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - 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/). | ||
| - A Cloudflare API token with the `Queues Edit` permission. | ||
|
|
||
|
|
||
| ### 1. Send a test message | ||
|
|
||
| To make sure you successfully authenticate and write a message to your queue, use `curl` on the command line: | ||
|
|
||
| ```sh | ||
| # Make sure to replace the placeholder with your shared secret | ||
| 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" } }' | ||
| ``` | ||
|
|
||
| ```sh output | ||
| {"success":true} | ||
| ``` | ||
|
|
||
| This will issue a HTTP POST request, and if successful, return a HTTP 200 with a `success: true` response body. | ||
|
|
||
| - If you receive a HTTP 403, this is because your API token is invalid or does not have the `Queues Edit` permission. | ||
|
|
||
| For full documentation about the HTTP Push API, refer to the [Cloudflare API documentation](https://developers.cloudflare.com/api/resources/queues/subresources/messages/). |
101 changes: 101 additions & 0 deletions
101
src/content/docs/queues/examples/publish-to-a-queue-via-workers.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| --- | ||
| title: Publish to a Queue via Workers | ||
sdnts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| summary: Publish to a Queue directly from your Worker. | ||
| pcx_content_type: example | ||
| sidebar: | ||
| order: 30 | ||
| head: | ||
| - tag: title | ||
| content: Queues - Publish Directly via a Worker | ||
| description: Publish to a Queue directly from your Worker. | ||
| --- | ||
|
|
||
| import { WranglerConfig } from "~/components"; | ||
|
|
||
| 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. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - 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/). | ||
| - A [configured **producer** binding](/queues/configuration/configure-queues/#producer-worker-configuration) in the Cloudflare dashboard or Wrangler file. | ||
|
|
||
| Configure your Wrangler file as follows: | ||
|
|
||
| <WranglerConfig> | ||
|
|
||
| ```toml | ||
| name = "my-worker" | ||
|
|
||
| [[queues.producers]] | ||
| queue = "my-queue" | ||
| binding = "YOUR_QUEUE" | ||
|
|
||
| ``` | ||
|
|
||
| </WranglerConfig> | ||
|
|
||
| ### 1. Create the Worker | ||
|
|
||
| The following Worker script: | ||
|
|
||
| 1. Validates that the request body is valid JSON. | ||
| 2. Publishes the payload to the queue. | ||
|
|
||
| ```ts | ||
| interface Env { | ||
| YOUR_QUEUE: Queue; | ||
| } | ||
|
|
||
| export default { | ||
| async fetch(req, env): Promise<Response> { | ||
| // Validate the payload is JSON | ||
| // In a production application, we may more robustly validate the payload | ||
| // against a schema using a library like 'zod' | ||
| let messages; | ||
| try { | ||
| messages = await req.json(); | ||
| } catch (e) { | ||
| // Return a HTTP 400 (Bad Request) if the payload isn't JSON | ||
| return Response.json({ err: "payload not valid JSON" }, { status: 400 }); | ||
| } | ||
|
|
||
| // Publish to the Queue | ||
| try { | ||
| await env.YOUR_QUEUE.send(messages); | ||
| } catch (e: any) { | ||
| console.log(`failed to send to the queue: ${e}`); | ||
| // Return a HTTP 500 (Internal Error) if our publish operation fails | ||
| return Response.json({ error: e.message }, { status: 500 }); | ||
| } | ||
|
|
||
| // Return a HTTP 200 if the send succeeded! | ||
| return Response.json({ success: true }); | ||
| }, | ||
| } satisfies ExportedHandler<Env>; | ||
| ``` | ||
|
|
||
| To deploy this Worker: | ||
|
|
||
| ```sh | ||
| npx wrangler deploy | ||
| ``` | ||
|
|
||
| ### 2. Send a test message | ||
|
|
||
| To make sure you successfully write a message to your queue, use `curl` on the command line: | ||
|
|
||
| ```sh | ||
| # Make sure to replace the placeholder with your shared secret | ||
| curl -XPOST "https://YOUR_WORKER.YOUR_ACCOUNT.workers.dev" --data '{"messages": [{"msg":"hello world"}]}' | ||
| ``` | ||
|
|
||
| ```sh output | ||
| {"success":true} | ||
| ``` | ||
|
|
||
| This will issue a HTTP POST request, and if successful, return a HTTP 200 with a `success: true` response body. | ||
|
|
||
| - If you receive a HTTP 400, this is because you attempted to send malformed JSON to your queue. | ||
| - If you receive a HTTP 500, this is because the message was not written to your Queue successfully. | ||
|
|
||
| You can use [`wrangler tail`](/workers/observability/logs/real-time-logs/) to debug the output of `console.log`. | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.