Use Serverless Kafka to Produce Events in Cloudflare Workers
In this tutorial, we will produce events to Upstash Kafka from a Cloudflare Workers function.
Create Kafka
First, create an Upstash Kafka cluster and topic following those steps. You will need the endpoint, username and password in the following steps.
Create Project
We will use
Wrangler to
create the application. After installing and configuring wrangler, create a
folder for your project inside the folder run: wrangler init
It will create wrangler.toml
. Paste your account id to the toml which is
logged by wrangler.
Copy and paste the Upstash Kafka URL, topic name, username and password to the toml.
Implement the Function
Init a node project and install @upstash/kafka:
Add the index.js as below:
The above code simply sends the message to Kafka. If your message is more complicated then you can send it in the request body as explained here.
Run and Deploy the Function
Run the function locally: wrangler dev
Deploy your function to Cloudflare by running:
This command will output your URL. The output of the URL should be something like this:
Test the Function
Now let’s validate that the messages are pushed to Kafka. We can consume the Kafka topic using the REST API. You can copy the curl code to consume from the Upstash Console.
upstash-kafka vs other Kafka Clients
Upstash also supports native Kafka clients (e.g. KafkaJS). But Cloudflare Workers runtime does not allow TCP connections. upstash-kafka is HTTP based. That’s why we use upstash-kafka in our Cloudflare examples.
Was this page helpful?