# Cloudflare Worker templates, deployed in one click

> Ready-made Workers that run on your own Cloudflare account, next to your site. Press Deploy to Cloudflare, enter your app key and secret, and the Worker handles one job at the edge: signing private and presence channels for your logged-in users, or holding visitors in a waiting room. Your channels, queues and settings stay in your Hibira account.

Source: https://hibira.io/guides/cloudflare

Ready-made Workers that run on your own Cloudflare account, next to your site. Press Deploy to Cloudflare, enter your app key and secret, and the Worker handles one job at the edge: signing private and presence channels for your logged-in users, or holding visitors in a waiting room. Your channels, queues and settings stay in your Hibira account.

Package: Cloudflare Worker templates

- Channel auth: private and presence channels for sites and apps with no backend. It checks the token your login already issues (Clerk, Auth0, Supabase, Firebase, Cloudflare Access or your own) and signs only the channels your rules allow, such as each user’s own private-user channel.
- Waiting room: a queue in front of any page behind Cloudflare, with no code changes, on any Cloudflare plan.
- Sending without a backend: with PUBLISH set, signed-in users send chat messages, votes and reactions through the channel auth Worker’s /publish. It fills in the sender from the login token, so nobody can post as someone else.
- AI streaming: the backend for an AI chat built with useChat(). Answers from Workers AI, Anthropic or any OpenAI-compatible model reach every tab over a channel, keep going after the tab closes and pick up again after a reload.
- Each template is a folder in one repository, with its settings explained and a way to deploy without the button. All of them fit the Workers free plan.

## 1. Install

```text
https://deploy.workers.cloudflare.com/?url=https://github.com/hibira-io/cloudflare/tree/main/channel-auth
  → sign in to Cloudflare and GitHub
  → HIBIRA_KEY, HIBIRA_SECRET: from your app page
  → JWKS_URL: your login's public keys, for example
    https://YOUR-APP.clerk.accounts.dev/.well-known/jwks.json
  → CHANNELS: private-user-{id},presence-*
  → Deploy
```

## 2. Use it

```js
import Pusher from 'pusher-js';

const auth = 'https://hibira-channel-auth.YOUR-SUBDOMAIN.workers.dev';
const headersProvider = () => ({ Authorization: `Bearer ${getToken()}` });

const pusher = new Pusher('YOUR_APP_KEY', {
  cluster: 'hkg',
  wsHost: 'ws.hibira.io',
  enabledTransports: ['ws', 'wss'],
  channelAuthorization: { endpoint: `${auth}/pusher/auth`, headersProvider },
  userAuthentication: { endpoint: `${auth}/pusher/user-auth`, headersProvider },
});

pusher.subscribe(`private-user-${userId}`).bind('order.shipped', (order) => {
  console.log('Your order shipped', order);
});
```

## 3. Send events

```js
// PUBLISH: presence-room-*   PUBLISH_EVENTS: message   HIBIRA_APP_ID: your app ID
await fetch(`${auth}/publish`, {
  method: 'POST',
  headers: { 'Content-Type': 'application/json', ...headersProvider() },
  body: JSON.stringify({ channel: 'presence-room-1', event: 'message', data: { text: 'Hello' } }),
});

// Everyone on the channel gets data.sender from the token: { id, name, picture }
pusher.subscribe('presence-room-1').bind('message', ({ text, sender }) => show(sender.name, text));
```

## Before you start

- getToken() returns your login’s current token, for example await clerk.session.getToken() or the access_token of a Supabase session. The Worker checks its signature, expiry, issuer and audience before signing anything.
- In CHANNELS, {id} is the user’s ID, {claim} any claim in the token and * the rest of the name, so private-org-{org_id}-* lets members of an organisation share its channels.
- Your app secret is stored as a Worker secret and never reaches the browser. The Worker keeps no data.
- Typing hints, cursors and other things that are fine to miss can go straight from the browser as client- events on a private or presence channel, once Client events is on in the app’s settings.
- Messages that start from a change in your data are better sent from where the change happens: a Supabase Edge Function, a Firebase Cloud Function, or the Hibira nodes for n8n, Make, Activepieces and Node-RED.
- Open the Worker’s own address in a browser to see whether its setup is complete and which settings are missing. The page is in your language and never shows a setting’s value.

- [GitHub](https://github.com/hibira-io/cloudflare)
- [Deploy to Cloudflare](https://deploy.workers.cloudflare.com/?url=https://github.com/hibira-io/cloudflare/tree/main/channel-auth)
