# brayden/pubsub architecture

> Architecture diagram of the GitHub repository brayden/pubsub, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/brayden/pubsub
- Repository: https://github.com/brayden/pubsub

## Overview

1. **Project shape**
   - This is a small TypeScript-based **Cloudflare Workers** project named `pubsub`.
   - The repository is centered around an **edge runtime service** with **Durable Objects** for stateful coordination, which strongly suggests a pub/sub or MQTT-style broker implementation.

2. **Runtime boundary**
   - `src/index.ts` is the main worker entry point.
   - `wrangler.jsonc` and `worker-configuration.d.ts` indicate the app is deployed and bound through the Cloudflare Workers platform.
   - The worker is the public ingress: it receives requests and delegates stateful behavior to Durable Objects.

3. **Stateful core**
   - `src/durable-objects/topic.ts` and `src/durable-objects/client.ts` are the two main stateful subsystems.
   - `topic.ts` likely manages per-topic coordination, subscription lists, and message fan-out.
   - `client.ts` likely manages per-client/session state such as connection lifecycle, identity, or delivery bookkeeping.
   - These two Durable Objects form the main architectural boundary between stateless request handling and persistent coordination.

4. **Request/message flow**
   - Incoming traffic enters the worker in `src/index.ts`.
   - The worker likely resolves the relevant Durable Object instance by topic or client identity, then forwards the request/message to it.
   - Durable Objects then handle state transitions and coordination without needing an external database.

5. **Protocol/domain model**
   - `src/types/mqtt.ts` indicates a dedicated MQTT protocol/domain type layer.
   - That suggests protocol packets, message shapes, or broker semantics are represented explicitly rather than as ad hoc objects.
   - The architecture is therefore protocol-aware, not just generic pub/sub.

6. **Shared configuration types**
   - `src/types/env.ts` defines the worker’s runtime environment contract.
   - This is the boundary for Cloudflare bindings, secrets, and Durable Object namespaces.
   - In architecture terms, it separates platform configuration from broker logic.

7. **Client-facing surface**
   - `index.html` and `mqtt.html` are static HTML assets at the repo root.
   - They likely serve as a minimal UI/demo harness for interacting with the worker or MQTT broker.
   - These files are ancillary, not the core runtime, but they show there is a browser-facing interaction path.

8. **No traditional backend datastore**
   - There is no evidence of a database, queue, or cache layer in the tree.
   - Persistent/stateful behavior appears to be handled primarily by Cloudflare Durable Objects.
   - That makes the system edge-native and self-contained.

9. **Operational/deployment tooling**
   - `package.json`, `pnpm-lock.yaml`, and `tsconfig.json` define the build and TypeScript toolchain.
   - `wrangler.jsonc` is the deployment and environment definition for Cloudflare.
   - The architecture is therefore "TypeScript app + Cloudflare runtime" rather than a separate app server plus infrastructure stack.

10. **Likely architectural diagram framing**
   - A useful diagram would show:
     - browser/client or MQTT client on the left,
     - Cloudflare Worker as the entry edge,
     - two Durable Object classes as stateful coordination nodes,
     - shared type definitions and Cloudflare env bindings as the internal contract layer.
   - The key design idea is **stateless ingress, stateful coordination in Durable Objects**.

Main entry points: [`src/index.ts`](https://github.com/brayden/pubsub/blob/main/src/index.ts) (Worker), [`src/durable-objects/topic.ts`](https://github.com/brayden/pubsub/blob/main/src/durable-objects/topic.ts) (Topic DO), [`src/durable-objects/client.ts`](https://github.com/brayden/pubsub/blob/main/src/durable-objects/client.ts) (Client DO), [`src/types/env.ts`](https://github.com/brayden/pubsub/blob/main/src/types/env.ts) (Env Types)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_surface["Client surface"]
  node_index_html["Web UI<br/>html demo<br/>[index.html]"]
  node_mqtt_html["MQTT UI<br/>html demo<br/>[mqtt.html]"]
end

subgraph group_edge["Edge runtime"]
  node_worker_entry["Worker<br/>entrypoint<br/>[index.ts]"]
  node_dispatch{{"Dispatch<br/>routing"}}
  node_cloudflare{{"Cloudflare Workers<br/>runtime platform"}}
end

subgraph group_state["Stateful core"]
  node_topic_do[("Topic DO<br/>durable object<br/>[topic.ts]")]
  node_client_do[("Client DO<br/>durable object<br/>[client.ts]")]
end

subgraph group_contracts["Shared contracts"]
  node_mqtt_types["MQTT Types<br/>protocol model<br/>[mqtt.ts]"]
  node_env_types["Env Types<br/>runtime contract<br/>[env.ts]"]
end

subgraph group_tooling["Tooling"]
  node_wrangler["Wrangler<br/>deploy config<br/>[wrangler.jsonc]"]
  node_worker_dts["Worker Types<br/>platform typings"]
  node_package_json["Package<br/>tooling config<br/>[package.json]"]
  node_tsconfig["TS Config<br/>compiler config<br/>[tsconfig.json]"]
end

node_index_html -->|"calls"| node_worker_entry
node_mqtt_html -->|"calls"| node_worker_entry
node_worker_entry -->|"routes"| node_dispatch
node_dispatch -->|"topic flow"| node_topic_do
node_dispatch -->|"client flow"| node_client_do
node_topic_do -->|"uses"| node_mqtt_types
node_client_do -->|"uses"| node_mqtt_types
node_worker_entry -->|"binds"| node_env_types
node_topic_do -->|"binds"| node_env_types
node_client_do -->|"binds"| node_env_types
node_wrangler -->|"configures"| node_cloudflare
node_worker_dts -->|"types"| node_cloudflare
node_package_json -->|"builds"| node_tsconfig
node_package_json -->|"deploys"| node_wrangler
node_worker_entry -->|"runs on"| node_cloudflare
node_topic_do -->|"persists on"| node_cloudflare
node_client_do -->|"persists on"| node_cloudflare

click node_index_html "https://github.com/brayden/pubsub/blob/main/index.html"
click node_mqtt_html "https://github.com/brayden/pubsub/blob/main/mqtt.html"
click node_worker_entry "https://github.com/brayden/pubsub/blob/main/src/index.ts"
click node_topic_do "https://github.com/brayden/pubsub/blob/main/src/durable-objects/topic.ts"
click node_client_do "https://github.com/brayden/pubsub/blob/main/src/durable-objects/client.ts"
click node_mqtt_types "https://github.com/brayden/pubsub/blob/main/src/types/mqtt.ts"
click node_env_types "https://github.com/brayden/pubsub/blob/main/src/types/env.ts"
click node_wrangler "https://github.com/brayden/pubsub/blob/main/wrangler.jsonc"
click node_worker_dts "https://github.com/brayden/pubsub/blob/main/worker-configuration.d.ts"
click node_package_json "https://github.com/brayden/pubsub/blob/main/package.json"
click node_tsconfig "https://github.com/brayden/pubsub/blob/main/tsconfig.json"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_index_html,node_mqtt_html toneBlue
class node_worker_entry,node_dispatch,node_cloudflare toneAmber
class node_topic_do,node_client_do toneMint
class node_mqtt_types,node_env_types toneRose
class node_wrangler,node_worker_dts,node_package_json,node_tsconfig toneIndigo
```

## Components

### Client surface

Browser-facing demo and entry pages

- [Web UI](https://github.com/brayden/pubsub/blob/main/index.html): html demo · Primary browser-facing page for interacting with the broker · `index.html`
- [MQTT UI](https://github.com/brayden/pubsub/blob/main/mqtt.html): html demo · Alternative browser-facing MQTT interaction page · `mqtt.html`

### Edge runtime

Cloudflare Worker entrypoint and request handling

- [Worker](https://github.com/brayden/pubsub/blob/main/src/index.ts): entrypoint · Cloudflare Worker request entry and orchestration layer · `src/index.ts`
- **Dispatch**: routing · Resolves requests into the correct stateful target
- **Cloudflare Workers**: runtime platform · Edge runtime and Durable Objects platform boundary

### Stateful core

Durable Objects that coordinate pub/sub state

- [Topic DO](https://github.com/brayden/pubsub/blob/main/src/durable-objects/topic.ts): durable object · Per-topic coordination, subscriptions, and fan-out · `src/durable-objects/topic.ts`
- [Client DO](https://github.com/brayden/pubsub/blob/main/src/durable-objects/client.ts): durable object · Per-client session and delivery state · `src/durable-objects/client.ts`

### Shared contracts

Protocol and environment types shared across runtime boundaries

- [MQTT Types](https://github.com/brayden/pubsub/blob/main/src/types/mqtt.ts): protocol model · Protocol packet and message shapes · `src/types/mqtt.ts`
- [Env Types](https://github.com/brayden/pubsub/blob/main/src/types/env.ts): runtime contract · Worker environment and binding definitions · `src/types/env.ts`

### Tooling

Build and deployment configuration

- [Wrangler](https://github.com/brayden/pubsub/blob/main/wrangler.jsonc): deploy config · Cloudflare deployment and binding configuration · `wrangler.jsonc`
- [Worker Types](https://github.com/brayden/pubsub/blob/main/worker-configuration.d.ts): platform typings · Generated Cloudflare worker type declarations · `worker-configuration.d.ts`
- [Package](https://github.com/brayden/pubsub/blob/main/package.json): tooling config · Project scripts and dependency metadata · `package.json`
- [TS Config](https://github.com/brayden/pubsub/blob/main/tsconfig.json): compiler config · TypeScript compiler settings · `tsconfig.json`

## Connections

- Web UI → Worker: calls · Browser page interacts with the worker
- MQTT UI → Worker: calls · MQTT-oriented page interacts with the worker
- Worker → Dispatch: routes · Worker classifies inbound requests and forwards them
- Dispatch → Topic DO: topic flow · Topic-related work is delegated to topic state
- Dispatch → Client DO: client flow · Client/session-related work is delegated to client state
- Topic DO → MQTT Types: uses · Topic coordination uses MQTT protocol structures
- Client DO → MQTT Types: uses · Client coordination uses MQTT protocol structures
- Worker → Env Types: binds · Worker reads its platform environment contract
- Topic DO → Env Types: binds · Topic DO depends on configured bindings
- Client DO → Env Types: binds · Client DO depends on configured bindings
- Wrangler → Cloudflare Workers: configures · Deployment file defines the runtime boundary
- Worker Types → Cloudflare Workers: types · Type declarations mirror the platform runtime
- Package → TS Config: builds · Project scripts rely on TypeScript compilation
- Package → Wrangler: deploys · Project tooling targets Cloudflare deployment
- Worker → Cloudflare Workers: runs on · Entrypoint executes inside the Cloudflare edge runtime
- Topic DO → Cloudflare Workers: persists on · Stateful coordination is hosted by Durable Objects
- Client DO → Cloudflare Workers: persists on · Client state is hosted by Durable Objects
