{"id":33409207,"url":"https://github.com/yocjyet/count","last_synced_at":"2026-06-03T20:31:44.285Z","repository":{"id":325792362,"uuid":"1102391700","full_name":"yocjyet/count","owner":"yocjyet","description":"Total Visit Count and Realtime Visitor Count using Cloudflare Workers + Cloudflare D1 + Redis","archived":false,"fork":false,"pushed_at":"2025-11-23T13:00:40.000Z","size":29,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-11-23T13:14:43.890Z","etag":null,"topics":["counter","realtime","redis","serverless","visitor-counter","websocket"],"latest_commit_sha":null,"homepage":"https://count.yocjyet.dev","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yocjyet.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":"AGENTS.md","dco":null,"cla":null}},"created_at":"2025-11-23T11:32:44.000Z","updated_at":"2025-11-23T13:00:43.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/yocjyet/count","commit_stats":null,"previous_names":["yocjyet/count"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/yocjyet/count","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yocjyet%2Fcount","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yocjyet%2Fcount/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yocjyet%2Fcount/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yocjyet%2Fcount/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yocjyet","download_url":"https://codeload.github.com/yocjyet/count/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yocjyet%2Fcount/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33878990,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-03T02:00:06.370Z","response_time":59,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["counter","realtime","redis","serverless","visitor-counter","websocket"],"created_at":"2025-11-23T18:08:37.344Z","updated_at":"2026-06-03T20:31:44.279Z","avatar_url":"https://github.com/yocjyet.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Visitor Counter API\r\n\r\nA high-performance, serverless counter service built with Cloudflare Workers, Hono, and Effect.ts. It supports both persistent total counts (e.g., page views) and realtime active user counts via WebSockets.\r\n\r\n## Features\r\n\r\n- **Persistent Counters**: Store total counts reliably using Cloudflare D1 (SQLite).\r\n- **Realtime Counters**: Track active users in real-time using WebSockets and Redis.\r\n- **Admin Dashboard**: Built-in Web UI to manage counters and view statistics.\r\n- **Type-Safe**: Built with TypeScript and Effect.ts for robust error handling and logic.\r\n- **Serverless**: Designed to run on Cloudflare Workers.\r\n\r\n## Tech Stack\r\n\r\n- **Framework**: [Hono](https://hono.dev/)\r\n- **Runtime**: [Cloudflare Workers](https://workers.cloudflare.com/)\r\n- **Database**: [Cloudflare D1](https://developers.cloudflare.com/d1/) (SQLite)\r\n- **Realtime**: [Upstash Redis](https://upstash.com/) (via [Serverless Redis HTTP](https://github.com/hiett/serverless-redis-http) for local dev)\r\n- **Logic**: [Effect.ts](https://effect.website/)\r\n\r\n## API Reference\r\n\r\n### Total Counters (`/counters`)\r\n\r\nPersistent counters for things like page views, downloads, etc.\r\n\r\n| Method | Endpoint | Description | Auth Required |\r\n| :--- | :--- | :--- | :--- |\r\n| `POST` | `/` | Create a new counter. Body: `{ \"key\": \"my-counter\" }` | No |\r\n| `GET` | `/` | List all counter keys (text format). | No |\r\n| `GET` | `/:key` | Get the current value of a counter. | No |\r\n| `GET` / `PATCH` | `/:key/increment` | Increment a counter by 1. | No |\r\n| `PUT` | `/:key` | Set a counter to a specific value. Body: `{ \"val\": 123 }` | **Yes** |\r\n| `DELETE` | `/:key` | Delete a counter. | **Yes** |\r\n\r\n### Realtime Counters (`/realtime`)\r\n\r\nEphemeral counters for tracking active sessions.\r\n\r\n| Method | Endpoint | Description | Auth Required |\r\n| :--- | :--- | :--- | :--- |\r\n| `GET` | `/:key/connect` | Connect via WebSocket to track presence. | No |\r\n| `GET` | `/:key` | Get the current active count. | No |\r\n| `PUT` | `/:key` | Set the active count (simulated users). Body: `{ \"val\": 5 }` | **Yes** |\r\n| `DELETE` | `/:key` | Reset the active count. | **Yes** |\r\n\r\n### Admin (`/admin`)\r\n\r\n| Method | Endpoint | Description |\r\n| :--- | :--- | :--- |\r\n| `GET` | `/` | Access the Admin Dashboard (HTML). |\r\n\r\n**Authentication**: Admin endpoints require an `Authorization: Bearer \u003cADMIN_SECRET\u003e` header. The Admin UI handles this via a login prompt.\r\n\r\n## Example Usage\r\n\r\n### Persistent Counters\r\n\r\n**1. Create a new counter**\r\n\r\n#### cURL\r\n```bash\r\ncurl -X POST https://your-worker.workers.dev/counters \\\r\n  -H \"Content-Type: application/json\" \\\r\n  -d '{\"key\": \"page-views\"}'\r\n```\r\n\r\n#### JavaScript\r\n```javascript\r\nconst response = await fetch(\"https://your-worker.workers.dev/counters\", {\r\n  method: \"POST\",\r\n  headers: { \"Content-Type\": \"application/json\" },\r\n  body: JSON.stringify({ key: \"page-views\" }),\r\n});\r\nconst result = await response.text();\r\nconsole.log(result);\r\n```\r\n\r\n**2. Increment the counter**\r\n\r\nYou can use this endpoint directly in an `\u003cimg\u003e` tag or via a simple fetch request.\r\n\r\n#### cURL\r\n```bash\r\ncurl https://your-worker.workers.dev/counters/page-views/increment\r\n```\r\n\r\n#### JavaScript\r\n```javascript\r\nawait fetch(\"https://your-worker.workers.dev/counters/page-views/increment\");\r\n```\r\n\r\n**3. Get the current count**\r\n\r\n#### cURL\r\n```bash\r\ncurl https://your-worker.workers.dev/counters/page-views\r\n```\r\n\r\n#### JavaScript\r\n```javascript\r\nconst response = await fetch(\"https://your-worker.workers.dev/counters/page-views\");\r\nconst count = await response.text();\r\nconsole.log(count);\r\n```\r\n\r\n### Realtime Counters\r\n\r\n**Connect via WebSocket**\r\n\r\n#### JavaScript\r\n```javascript\r\nlet ws;\r\nlet reconnectInterval;\r\n\r\nfunction connect() {\r\n  ws = new WebSocket(\"wss://your-worker.workers.dev/realtime/active-users/connect\");\r\n\r\n  ws.onmessage = (event) =\u003e {\r\n    const count = Number(event.data);\r\n    console.log(\"Current active users:\", count);\r\n  };\r\n\r\n  ws.onopen = () =\u003e {\r\n    console.log(\"Connected\");\r\n    // Clear any pending reconnects\r\n    if (reconnectInterval) clearTimeout(reconnectInterval);\r\n    \r\n    // Keep alive\r\n    setInterval(() =\u003e {\r\n      if (ws.readyState === WebSocket.OPEN) ws.send(\"ping\");\r\n    }, 10000);\r\n  };\r\n\r\n  ws.onclose = () =\u003e {\r\n    console.log(\"Disconnected. Reconnecting in 3s...\");\r\n    reconnectInterval = setTimeout(connect, 3000);\r\n  };\r\n  \r\n  ws.onerror = (err) =\u003e {\r\n    console.error(\"WebSocket error:\", err);\r\n    ws.close(); // Trigger onclose to reconnect\r\n  };\r\n}\r\n\r\n// Handle tab visibility changes\r\ndocument.addEventListener(\"visibilitychange\", () =\u003e {\r\n  if (document.visibilityState === \"visible\") {\r\n    if (!ws || ws.readyState === WebSocket.CLOSED) {\r\n      connect();\r\n    }\r\n  }\r\n});\r\n\r\n// Start connection\r\nconnect();\r\n```\r\n\r\n#### CLI (wscat)\r\n```bash\r\nnpx wscat -c wss://your-worker.workers.dev/realtime/active-users/connect\r\n```\r\n\r\n## Local Development\r\n\r\n### Prerequisites\r\n\r\n- [Node.js](https://nodejs.org/) (v18+)\r\n- [pnpm](https://pnpm.io/)\r\n- [Docker](https://www.docker.com/) (for local Redis)\r\n\r\n### Setup\r\n\r\n1.  **Install Dependencies**\r\n    ```bash\r\n    pnpm install\r\n    ```\r\n\r\n2.  **Initialize Database (D1)**\r\n    Creates the local SQLite database and applies the schema.\r\n    ```bash\r\n    npm run init:database:local\r\n    ```\r\n\r\n3.  **Start Local Redis**\r\n    Starts a standard Redis container and the Serverless Redis HTTP (SRH) proxy to mimic Upstash locally.\r\n    ```bash\r\n    # Start Redis\r\n    npm run init:redis:local\r\n    \r\n    # Start SRH Proxy\r\n    npm run init:redis:srh:local\r\n    ```\r\n\r\n4.  **Start Development Server**\r\n    ```bash\r\n    pnpm dev\r\n    ```\r\n    The API will be available at `http://localhost:7817`.\r\n\r\n### Environment Variables\r\n\r\nCreate a `.dev.vars` file for local secrets (optional, as defaults work for local dev):\r\n\r\n```ini\r\nADMIN_SECRET=your_secret_password\r\nUPSTASH_REDIS_REST_URL=http://127.0.0.1:15384\r\nUPSTASH_REDIS_REST_TOKEN=HELLOWORLD\r\n```\r\n\r\n## Deployment\r\n\r\nDeploy to Cloudflare Workers using Wrangler:\r\n\r\n```bash\r\npnpm deploy\r\n```\r\n\r\nEnsure you have set up the secrets in Cloudflare:\r\n\r\n```bash\r\nnpx wrangler secret put ADMIN_SECRET\r\nnpx wrangler secret put UPSTASH_REDIS_REST_URL\r\nnpx wrangler secret put UPSTASH_REDIS_REST_TOKEN\r\n```\r\n\r\n## License\r\n\r\nThis project is licensed under the [MIT License](./LICENSE).\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyocjyet%2Fcount","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyocjyet%2Fcount","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyocjyet%2Fcount/lists"}