Configure Workers for Platforms
This guide will instruct you on setting up Workers for Platforms. You will configure a dispatch namespace, a dynamic dispatch Worker and a user Worker to test a request end to end. This guide assumes that you already have a Cloudflare account. If you do not have a Cloudflare account, sign up before continuing.
Prerequisite: Enable Workers for Platforms
Workers for Platforms is available for Enterprise customers only. To enable Workers for Platforms, contact your Cloudflare account team.
1. Install C3
C3 (create-cloudflare-cli) is a command-line tool designed to help you setup and deploy Workers to Cloudflare as fast as possible.
Open a terminal window and run C3 to create your Worker project called customer-worker-1
:
$ npm create cloudflare@latest customer-worker-1 -- --type=hello-world
When setting up your customer-worker-1
Worker, answer the questions as below:
- Select
no
to using TypeScript. - Select
no
to using Git for version control. - Select
no
to deploying.
2. Create a dispatch namespace
Create a dispatch namespace. A dispatch namespace is made up of a collection of user Workers. User Workers are Workers that your end users (end developers) create.
To create a dispatch namespace, run:
$ npx wrangler dispatch-namespace create <NAMESPACE_NAME>
3. Upload a user Worker to the dispatch namespace
To upload a user Worker to the dispatch namespace, deploy your application to the dispatch namespace by running the following command:
$ npx wrangler deploy --dispatch-namespace <NAMESPACE_NAME>
4. Create a dispatch Worker
User Workers are written by end developers. End developers can deploy user Workers to script automated actions, create integrations or modify response payload to return custom content.
You will now give your dispatch Worker the logic it needs to route to the user Worker created in step 2.
Create your dispatch Worker:
$ npx wrangler init my-dispatcher
When setting up your my-dispatcher
Worker, answer the questions as below:
- Choose
"Hello World" Worker
as the type of application. - Select
no
to using TypeScript. - Select
no
to using Git for version control. - Select
no
to deploying.
Change to your project’s directory:
$ cd my-dispatcher
Open the wrangler.toml
file in your project directory, and add the dispatch namespace binding:
wrangler.toml[[dispatch_namespace]]
binding = "dispatcher"
namespace = "<NAMESPACE_NAME>"
Add the followinng to the index.js file:
index.jsexport default { async fetch(req, env) { const worker = env.dispatcher.get("customer-worker-1"); return await worker.fetch(req); },
};
5. Test a request
You will now send a request to the route your dynamic dispatch Worker is on.
Open your terminal and run the following command to test your dynamic dispatch Worker:
$ npx wrangler deploy
You should receive the response (Hello world
) you created in your user Worker (customer-worker-1
) that you call from your dynamic dispatch Worker (the Worker you made in step 3).
Preview your Workers for Platforms project at https://my-dispatcher.<YOUR_WORKER_SUBDOMAIN>.workers.dev/
.
By completing this guide, you have successfully set up a dispatch namespace, dynamic dispatch Worker and user Worker to test a request end to end.