Skip to content

Commit db9e181

Browse files
committed
Added Worker Bindings docs
1 parent cc97092 commit db9e181

File tree

3 files changed

+125
-1
lines changed

3 files changed

+125
-1
lines changed

src/content/docs/pipelines/get-started.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ Pipelines handle batching the data, so you can continue posting data to the Pipe
8383

8484
## 4. Verify in R2
8585

86-
Go to the R2 bucket you created in step 1 via [Cloudflare dashboard](https://dash.cloudflare.com/). You should see a prefix for today's date. Click through, and you will see a file created containing the JSON data you posted in step 3.
86+
Go to the R2 bucket you created in step 1 via [the Cloudflare dashboard](https://dash.cloudflare.com/). You should see a prefix for today's date. Click through, and you will see a file created containing the JSON data you posted in step 3.
8787

8888
## Summary
8989

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
pcx_content_type: navigation
3+
title: Sources
4+
sidebar:
5+
order: 3
6+
group:
7+
hideIndex: false
8+
---
9+
10+
import { DirectoryListing } from "~/components"
11+
12+
<DirectoryListing />
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
---
2+
title: Worker Bindings
3+
pcx_content_type: concept
4+
sidebar:
5+
order: 1
6+
head:
7+
- tag: title
8+
content: Worker Bindings
9+
---
10+
11+
import { Render, PackageManagers } from "~/components"
12+
13+
You can send records to your Pipeline directly from a [Cloudflare Worker](workers/). To do so, you need to:
14+
1. Create a Worker
15+
2. Create a Pipeline
16+
3. Add your Pipeline as a binding in your Workers' `wrangler.toml` file
17+
4. Write your worker, to send records to your Pipeline
18+
19+
## 1. Create a Worker
20+
Create a Cloudflare Worker if you don't already have one. This Worker will send records to your Pipeline.
21+
22+
To create a Worker, run:
23+
24+
<PackageManagers
25+
type="create"
26+
pkg="cloudflare@latest"
27+
args={"pipeline-worker"}
28+
/>
29+
30+
<Render
31+
file="c3-post-run-steps"
32+
product="workers"
33+
params={{
34+
one: "Hello World example",
35+
two: "Hello World Worker",
36+
three: "TypeScript",
37+
}}
38+
/>
39+
40+
This will create a new directory, which will include both a `src/index.ts` Worker script, and a [`wrangler.toml`](/workers/wrangler/configuration/) configuration file. Navigate into the newly created directory:
41+
42+
```sh
43+
cd pipeline-worker
44+
```
45+
46+
## 2. Create a Pipeline
47+
Create a new Pipeline, if you don't already have one. Follow the instructions in the (get started guide)[pipelines/get-started] if this is your first time creating a Pipeline.
48+
49+
By default, Worker bindings are enabled on all Pipelines. Keep track of the name you gave your Pipeline in this stage; we'll use it in the next step.
50+
51+
## 3. Add a Binding
52+
To connect your Worker to your Pipeline, you need to create a binding. [Bindings](workers/runtime-apis/bindings/) allow you to grant specific capabilities to your Worker.
53+
54+
Open your newly generated `wrangler.toml` configuration file and add the following:
55+
56+
```toml
57+
[[pipelines]]
58+
binding = "MY_PIPELINE"
59+
queue = "<MY-PIPELINE-NAME>"
60+
```
61+
62+
Replace `<MY-PIPELINE-NAME>` with the name of the Pipeline you created in step 2. Next, replace `MY_PIPELINE` with the name you want for your `binding`. The binding must be a valid JavaScript variable name. This is the variable you will use to reference this queue in your Worker.
63+
64+
## 4. Write your Worker
65+
You will now configure your Worker to send records to your Pipeline. Your worker will:
66+
67+
1. Take a request it receives from the browser
68+
2. Transform the request to JSON
69+
3. Send the resulting record to your Pipeline
70+
71+
In your Worker project directory, open the `src` folder and add the following to your `index.ts` file:
72+
```ts
73+
export interface Env {
74+
<MY_PIPELINE>: Pipeline<any>;
75+
}
76+
77+
export default {
78+
async fetch(req, env, ctx): Promise<Response> {
79+
let record = {
80+
url: req.url,
81+
method: req.method,
82+
headers: Object.fromEntries(req.headers)
83+
}
84+
await env.MY_PIPELINE.send([record]);
85+
return new Response('Success');
86+
},
87+
} satisfies ExportedHandler<Env>;
88+
```
89+
90+
Replace `MY_PIPELINE` with the name of the binding you set in Step 3. If sending the record to the Pipeline fails, your Worker will return an error (raise an exception). If sending the record succeeds, it will return `Success` back with a HTTP `200` status code to the browser.
91+
92+
In a production application, you would likely use a [`try...catch`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch) statement to catch the exception and handle it directly (for example, return a custom error or even retry).
93+
94+
### Publish your Worker
95+
With your `wrangler.toml` file and `index.ts` file configured, you are ready to publish your producer Worker. To publish your producer Worker, run:
96+
97+
```sh
98+
npx wrangler deploy
99+
```
100+
101+
You should see output that resembles the below, with a `*.workers.dev` URL by default.
102+
103+
```
104+
Uploaded <YOUR-WORKER-NAME> (0.76 sec)
105+
Published <YOUR-WORKER-NAME> (0.29 sec)
106+
https://<YOUR-WORKER-NAME>.<YOUR-ACCOUNT>.workers.dev
107+
```
108+
109+
Copy your `*.workers.dev` subdomain and paste it into a new browser tab. Refresh the page a few times to send records to your Pipeline. Your browser should return the `Success` response after sending the record to your Pipeline.
110+
111+
## 5. Verify in R2
112+
Go to the R2 bucket you created in step 2 via [the Cloudflare dashboard](https://dash.cloudflare.com/). You should see a prefix for today's date. Click through, and you'll find one or more files, containing the records you sent in step 4.

0 commit comments

Comments
 (0)