Application built using Hono and deployed on Cloudflare Workers.
The project uses:
- Cloudflare Durable Objects to manage WebSocket sessions
- Cloudflare KV to store chat history and sessions state, logs, meta information, etc
- Node.js 18+
- pnpm, yarn or npm
- Cloudflare account (paid version)
- Cloudflare CLI (
wrangler
) - (optional) wscat for testing WebSocket locally
nvm use # if you use NVM or select node v22 manually
npm install
cp .dev.vars.example .dev.vars
# TODO: npm test (WIP)
wrangler dev
npm install -g wscat
wscat -H "Authorization: Bearer honoiscool" -c ws://localhost:8787/ws/hello_session
{ "action": "start", "character": "Santa" }
{ "action": "start" }
For this case: Joi will be by default
curl -H "Authorization: Bearer honoiscool" "http://localhost:8787/sessions?pretty"
curl -H "Authorization: Bearer honoiscool" "http://localhost:8787/sessions/:sessionIdFromResponseAbove?pretty"
curl -H "Authorization: Bearer honoiscool" "http://localhost:8787/sessions/:sessionIdFromResponseAbove/characters/active?pretty"
curl -H "Authorization: Bearer honoiscool" "http://localhost:8787/logs?pretty"
Create the required Cloudflare KV namespaces:
wrangler kv:namespace create "COMMAND_INC_LOGS"
wrangler kv:namespace create "COMMAND_INC_HISTORY"
Set your IDs for KVs to wrangler.jsonc
"kv_namespaces": [
{ "binding": "COMMAND_INC_LOGS", "id": "COMMAND_INC_LOGS_ID" },
{ "binding": "COMMAND_INC_HISTORY", "id": "COMMAND_INC_HISTORY_ID" },
],
Cloudflare Durable Object COMMAND_INC_DO
will be automatically created after the first deploy of your application.
You can change the name for DO in wrangler.jsonc
Set your JWT_TOKEN
for token signing:
wrangler secret put JWT_TOKEN
This will run your Hono app locally in Cloudflare's development environment.
wrangler deploy
npm install -g wscat
wscat -H "Authorization: Bearer secret_token_from_2.3" -c wss://worker_cloudflare_host/ws/hello_cloud_session
{ "action": "start", "character": "Santa" }
curl -H "Authorization: Bearer secret_token_from_2.3" "https://worker_cloudflare_host/sessions?pretty"
curl -H "Authorization: Bearer honoiscool" "https://worker_cloudflare_host/sessions/:sessionIdFromResponseAbove?pretty"
curl -H "Authorization: Bearer honoiscool" "https://worker_cloudflare_host/sessions/:sessionIdFromResponseAbove/characters/active?pretty"
curl -H "Authorization: Bearer honoiscool" "https://worker_cloudflare_host/logs?pretty"
- All endpoints requiring authentication use JWT passed via
Authorization: Bearer <token>
header.
- (Solution for demo) Store secrets like
JWT_TOKEN
using:
wrangler secret put JWT_TOKEN