Skip to content

Commit 5015a5c

Browse files
committed
Updated getting started guide
1 parent 807437d commit 5015a5c

File tree

1 file changed

+88
-1
lines changed

1 file changed

+88
-1
lines changed

src/content/docs/queues/event-subscriptions/getting-started.mdx

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,92 @@ We can verify that the event has been delivered by previewing the message. We'll
6464
Now, we know that our event subscription has been created, and that events are being delivered successfully to our queue.
6565

6666
## 3. Setup a consumer worker to consume the event
67-
Finally, we are going to setup a Consumer Worker to consume this event.
67+
Finally, we are going to setup a consumer [Worker](/workers/). If you have used queues before, these steps will look familiar to you. But if you've never used queues, continue to follow this tutorial.
6868

69+
We're going to setup a new Worker, which will consume events from the queue we setup in step 1. A consumer Worker receives messages from your queue. When the consumer Worker receives your queue's messages, it can take actions, such as updating a database, sending you an email, or more.
70+
71+
:::note
72+
73+
Queues also supports [pull-based consumers](/queues/configuration/pull-consumers/), which allows any HTTP-based client to consume messages from a queue. This guide creates a push-based consumer using Cloudflare Workers.
74+
75+
:::
76+
77+
To create a consumer Worker, run:
78+
<PackageManagers
79+
type="create"
80+
pkg="cloudflare@latest"
81+
args={"consumer-worker"}
82+
/>
83+
84+
<Render
85+
file="c3-post-run-steps"
86+
product="workers"
87+
params={{
88+
category: "hello-world",
89+
type: "Worker only",
90+
lang: "TypeScript",
91+
}}
92+
/>
93+
94+
This will create a new directory, which will include both a `src/index.ts` Worker script, and a [`wrangler.jsonc`](/workers/wrangler/configuration/) configuration file.
95+
96+
Move into the newly created directory:
97+
```sh
98+
cd consumer-worker
99+
```
100+
101+
Now we're going to setup the queue handler. When messages are pushed from your queue to your consumer worker, it is done by invoking the queue handler. Open the `index.ts` file, and add the following `queue` handler to your existing `fetch` handler:
102+
```ts
103+
export default {
104+
async fetch(request, env, ctx): Promise<Response> {
105+
return new Response('Hello World!');
106+
},
107+
async queue(
108+
batch: MessageBatch<EventMessageWorkflows>, env: Environment, ctx: ExecutionContext): Promise<void> {
109+
let messages = JSON.stringify(batch.messages);
110+
console.log(`consumed from our queue: ${messages}`);
111+
}
112+
} satisfies ExportedHandler<Env>;
113+
```
114+
115+
For the moment, this function will not do anything, as it is not connected to the queue.
116+
117+
### Connect the consumer Worker to your queue
118+
After you have configured your consumer Worker, you are ready to connect it to your queue.
119+
120+
Each queue can only have one consumer Worker connected to it. If you try to connect multiple consumers to the same queue, you will encounter an error when attempting to publish that Worker.
121+
122+
To connect your queue to your consumer Worker, open your `wrangler.jsonc` file and add this to the bottom.
123+
```json
124+
...
125+
"queues": {
126+
"consumers": [
127+
{
128+
"queue": "<MY-QUEUE-NAME>"
129+
}
130+
]
131+
}
132+
...
133+
```
134+
135+
Replace `MY-QUEUE-NAME` with the name of the queue you created in [step 1](/queues/event-subscriptions/getting-started#1-create-a-queue).
136+
137+
### Publish your consumer worker
138+
With your Wrangler configuration and `index.ts` file configured, publish your consumer Worker by running:
139+
140+
```sh
141+
npx wrangler deploy
142+
```
143+
144+
## 4. Read events from the Queue
145+
After you set up consumer Worker, you can read messages from the queue.
146+
147+
Run `wrangler tail` to start waiting for our consumer to log the messages it receives:
148+
149+
```sh
150+
npx wrangler tail
151+
```
152+
153+
TODO: trigger the workflow
154+
155+
By completing this guide, you have now created a Queue, Workflow, an event subscription, and a consumer Worker which consumes the events.

0 commit comments

Comments
 (0)