This is a pub/sub module made with Cloudflare Durable Objects.
This module will have 2 endpoints once it's online.
POST /channels/:channel/messages
GET /sub
Let's say the channel name is test
.
POST https://your_worker_url.com/channels/test/messages
Content-Type: application/json
X-Api-Key: key
{
"user": "alice",
"message": "hey bob!"
}
This will return:
{
"status": "ok"
}
This is an SSE endpoint.
GET https://your_worker_url.com/sub?channel=test&apiKey=key
Important
You need a paid plan of Cloudflare Workers ($5/month) to deploy this project.
-
Clone this repo
git clone https://github.com/jw-12138/durable-objects-channel.git
-
Install dependencies
npm i
-
Deploy to workers
npm run deploy
-
Set an environment variable for your worker, the key is called
API_KEY
[!IMPORTANT]
Make sure theAPI_KEY
is safe enough, otherwise anyone can access this API.
Now you can use the above APIs to publish and subscribe messages.