Skip to main content
Version: 0.11.12 (stable)

Integrate Cloudflare Workers with InfinyOn Cloud

Connect your Cloudflare workers with InfinyOn Cloud for powerful event-processing data pipelines. InfinyOn Cloud's robust data streaming allows you to seamlessly move and transform data and trigger actions.

In this guide, we'll build a simple CloudFlare worker that sends events to InfinyOn Cloud through the webhook API.

Use Cases

Prerequisites

To follow along you'll need the following:

Let's get started.

Create a Webhook

External services send events to InfinyOn Cloud via webhooks, connectors, and custom clients. In this example, we'll use webhooks.

Use Fluvio CLI to create a webhook on InfinyOn Cloud.

  1. Create a webhook configuration file cf-webhook.yaml:
meta:
name: cf-webhook
topic: cf-events

webhook:
outputParts: body
outputType: json
  1. Create the webhook endpoint:
$ fluvio cloud webhook create -c cf-webhook.yaml
Webhook "cf-webhook" created with url: https://infinyon.cloud/webhooks/v1/xyz

The command returns an endpoint that tells Cloudflare where InfinyOn is listening for events.

Use fluvio cloud webhook list to list all your webhooks.

Build a Cloudflare Worker

Cloudflare uses Wrangler, a command-line tool that helps developers build workers.

Install Wrangler

Use npm to install wrangler:

$ npm install -g wrangler

Create a Worker

Next, we'll create a directory, write the worker code, and provision a configuration file for wrangler to access our code.

  1. Create a project directory:
$ mkdir cf-infinyon; cd ./cf-infinyon
  1. Create a file index.js and add the following code:
const WEBHOOK_URL = "https://infinyon.cloud/webhooks/v1/xyz";

addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event.request));
});

async function handleRequest(request) {
let jsonData = await request.json();

const response = await fetch(WEBHOOK_URL, {
method: "POST",
headers: {
"Content-type": `application/json`,
},
body: JSON.stringify(jsonData)
}
);

const text_response = response.ok ? "" : "Webhook gateway error.";

return new Response(text_response, { status: response.status });
}

The worker fetches an event, retrieves its JSON payload, and forwards it to the webhook gateway on InfinyOn Cloud.

Note: Update the endpoint link with your own (see above).

  1. Add a wrangler configuration file file wrangler.toml and add the following settings:
name = "cf-infinyon"
main = "index.js"
compatibility_date = "2023-09-04"

We are all set to run the code, but first let's review the directory:

├── index.js
└── wrangler.toml

Test Cloudflare to InfinyOn Cloud Pipeline

With all the components provisioned, we should be ready to test our data pipeline end-to-end.

  1. Start the Cloudflare worker:
$ wrangler dev
Starting local server...
Ready on http://0.0.0.0:8787
  1. Start the InfinyOn Cloud consumer:
$ fluvio consume cf-events --output json
Consuming records from 'cf-events'

  1. Use curl to post an event:
$ curl -v -X POST http://0.0.0.0:8787 \
-H "Content-Type: application/json" \
-d '{"hello": "world!"}'
  1. The InfinyOn consumer show display the following event:
{
"hello": "world!"
}

Congratulations! 🎉 You have bridged Cloudflare workers with InfinyOn Cloud, the first set in building data reach event-driven services.

Next steps:

Reference