# rethinkdb/horizon architecture

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

- Interactive diagram: https://gitdiagram.com/rethinkdb/horizon
- Repository: https://github.com/rethinkdb/horizon

## Overview

Horizon is a realtime application backend with a JavaScript client, a server backed by RethinkDB, and a CLI for project setup and operation. The main runtime flow is a client request over the Horizon protocol, server dispatch to data or identity operations, and database access; subscriptions stream updates back to clients. The CLI provides a separate developer workflow for initialization, serving, schema management, and migration. The graph preserves documented authentication and permissions capabilities; some server subsystems are represented by their exact enclosing directories because their implementation excerpts were not available.

Main entry points: [`server/src/server.js`](https://github.com/rethinkdb/horizon/blob/next/server/src/server.js) (HTTP And WS Server), [`client/src/index.js`](https://github.com/rethinkdb/horizon/blob/next/client/src/index.js) (Client API), [`server/src/endpoint/query.js`](https://github.com/rethinkdb/horizon/blob/next/server/src/endpoint/query.js) (Query Endpoint), [`cli/src/main.js`](https://github.com/rethinkdb/horizon/blob/next/cli/src/main.js) (Command Router)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_cli["Developer CLI"]
  node_cli_entry["Command Router<br/>[main.js]"]
  node_init["Project Setup<br/>[init.js]"]
  node_serve["Serve App<br/>[serve.js]"]
  node_schema_cli["Schema Manager<br/>[schema.js]"]
  node_migration["Database Migration<br/>[migrate.js]"]
  node_cli_config["CLI Configuration<br/>[config.js]"]
  node_project_dir["Project Directory"]
  node_interrupt["Interrupt Handling<br/>[interrupt.js]"]
  node_rdb_launcher["Local DB Launcher"]
end

subgraph group_client["JavaScript Client"]
  node_client_api["Client API<br/>[index.js]"]
  node_query_ast["Query Builder<br/>[ast.js]"]
  node_client_socket["WebSocket Transport<br/>[socket.js]"]
  node_client_serialization["Wire Serialization<br/>[serialization.js]"]
  node_client_auth["Client Authentication<br/>[auth.js]"]
  node_client_model["Client Models<br/>[model.js]"]
end

subgraph group_server["Horizon Server"]
  node_server["HTTP And WS Server<br/>[server.js]"]
  node_ws_client["Protocol Client<br/>[client.js]"]
  node_request["Request Lifecycle<br/>[request.js]"]
  node_protocol["Protocol Schemas"]
  node_query_endpoint["Query Endpoint<br/>[query.js]"]
  node_write_endpoints["Write Endpoints"]
  node_subscribe_endpoint["Subscription Endpoint<br/>[subscribe.js]"]
  node_metadata["Collection Metadata"]
  node_reql["RethinkDB Connection<br/>[reql_connection.js]"]
end

subgraph group_access["Identity And Access"]
  node_auth_server["Authentication Service<br/>[auth.js]"]
  node_auth_providers["Provider Adapters"]
  node_permissions["Permission Rules"]
  node_identity["User Identity<br/>[metadata.js]"]
end

node_developer(("Developer"))
node_database[("RethinkDB")]
node_auth_providers_ext["Auth Providers"]

node_developer -->|"runs command"| node_cli_entry
node_cli_entry -->|"dispatches"| node_init
node_cli_entry -->|"dispatches"| node_serve
node_cli_entry -->|"dispatches"| node_schema_cli
node_cli_entry -->|"dispatches"| node_migration
node_serve -->|"loads settings"| node_cli_config
node_serve -->|"changes directory"| node_project_dir
node_serve -->|"starts local DB"| node_rdb_launcher
node_serve -.->|"starts server"| node_server
node_schema_cli -->|"loads settings"| node_cli_config
node_schema_cli -->|"uses metadata"| node_metadata
node_migration -->|"reuses schema save"| node_schema_cli
node_migration -->|"loads settings"| node_cli_config
node_migration -->|"changes directory"| node_project_dir
node_migration -->|"starts local DB"| node_rdb_launcher
node_developer -->|"uses API"| node_client_api
node_client_api -->|"builds queries"| node_query_ast
node_client_api -->|"sends requests"| node_client_socket
node_client_api -->|"handles tokens"| node_client_auth
node_client_api -->|"exposes models"| node_client_model
node_client_socket -->|"serializes messages"| node_client_serialization
node_client_api -->|"uses protocol"| node_server
node_server -->|"creates clients"| node_ws_client
node_server -->|"opens connection"| node_reql
node_server -->|"configures auth"| node_auth_server
node_server -->|"registers endpoint"| node_query_endpoint
node_server -->|"registers endpoints"| node_write_endpoints
node_server -->|"registers endpoint"| node_subscribe_endpoint
node_ws_client -->|"validates messages"| node_protocol
node_ws_client -->|"creates requests"| node_request
node_query_endpoint -->|"validates query"| node_protocol
node_query_endpoint -->|"reads collections"| node_metadata
node_query_endpoint -.->|"checks rules"| node_permissions
node_query_endpoint -->|"runs query"| node_database
node_write_endpoints -.->|"writes records"| node_database
node_subscribe_endpoint -.->|"reuses query"| node_query_endpoint
node_reql -->|"connects"| node_database
node_auth_server -->|"uses adapters"| node_auth_providers
node_auth_server -.->|"manages accounts"| node_identity
node_auth_providers -.->|"integrates"| node_auth_providers_ext
node_auth_server -.->|"authorizes access"| node_permissions

click node_cli_entry "https://github.com/rethinkdb/horizon/blob/next/cli/src/main.js"
click node_init "https://github.com/rethinkdb/horizon/blob/next/cli/src/init.js"
click node_serve "https://github.com/rethinkdb/horizon/blob/next/cli/src/serve.js"
click node_schema_cli "https://github.com/rethinkdb/horizon/blob/next/cli/src/schema.js"
click node_migration "https://github.com/rethinkdb/horizon/blob/next/cli/src/migrate.js"
click node_cli_config "https://github.com/rethinkdb/horizon/blob/next/cli/src/utils/config.js"
click node_project_dir "https://github.com/rethinkdb/horizon/blob/next/cli/src/utils/change_to_project_dir.js"
click node_interrupt "https://github.com/rethinkdb/horizon/blob/next/cli/src/utils/interrupt.js"
click node_rdb_launcher "https://github.com/rethinkdb/horizon/blob/next/cli/src/utils/start_rdb_server.js"
click node_client_api "https://github.com/rethinkdb/horizon/blob/next/client/src/index.js"
click node_query_ast "https://github.com/rethinkdb/horizon/blob/next/client/src/ast.js"
click node_client_socket "https://github.com/rethinkdb/horizon/blob/next/client/src/socket.js"
click node_client_serialization "https://github.com/rethinkdb/horizon/blob/next/client/src/serialization.js"
click node_client_auth "https://github.com/rethinkdb/horizon/blob/next/client/src/auth.js"
click node_client_model "https://github.com/rethinkdb/horizon/blob/next/client/src/model.js"
click node_server "https://github.com/rethinkdb/horizon/blob/next/server/src/server.js"
click node_ws_client "https://github.com/rethinkdb/horizon/blob/next/server/src/client.js"
click node_request "https://github.com/rethinkdb/horizon/blob/next/server/src/request.js"
click node_protocol "https://github.com/rethinkdb/horizon/blob/next/server/src/schema/horizon_protocol.js"
click node_query_endpoint "https://github.com/rethinkdb/horizon/blob/next/server/src/endpoint/query.js"
click node_write_endpoints "https://github.com/rethinkdb/horizon/tree/next/server/src/endpoint"
click node_subscribe_endpoint "https://github.com/rethinkdb/horizon/blob/next/server/src/endpoint/subscribe.js"
click node_metadata "https://github.com/rethinkdb/horizon/tree/next/server/src/metadata"
click node_reql "https://github.com/rethinkdb/horizon/blob/next/server/src/reql_connection.js"
click node_auth_server "https://github.com/rethinkdb/horizon/blob/next/server/src/auth.js"
click node_auth_providers "https://github.com/rethinkdb/horizon/tree/next/server/src/auth"
click node_permissions "https://github.com/rethinkdb/horizon/tree/next/server/src/permissions"
click node_identity "https://github.com/rethinkdb/horizon/blob/next/server/src/metadata/metadata.js"

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_cli_entry,node_init,node_serve,node_schema_cli,node_migration,node_cli_config,node_project_dir,node_interrupt,node_rdb_launcher toneBlue
class node_client_api,node_query_ast,node_client_socket,node_client_serialization,node_client_auth,node_client_model,node_database toneAmber
class node_server,node_ws_client,node_request,node_protocol,node_query_endpoint,node_write_endpoints,node_subscribe_endpoint,node_metadata,node_reql toneMint
class node_auth_server,node_auth_providers,node_permissions,node_identity toneRose
class node_developer,node_auth_providers_ext toneIndigo
```

## Components

### Developer CLI

- [Command Router](https://github.com/rethinkdb/horizon/blob/next/cli/src/main.js): `cli/src/main.js`
- [Project Setup](https://github.com/rethinkdb/horizon/blob/next/cli/src/init.js): `cli/src/init.js`
- [Serve App](https://github.com/rethinkdb/horizon/blob/next/cli/src/serve.js): `cli/src/serve.js`
- [Schema Manager](https://github.com/rethinkdb/horizon/blob/next/cli/src/schema.js): `cli/src/schema.js`
- [Database Migration](https://github.com/rethinkdb/horizon/blob/next/cli/src/migrate.js): `cli/src/migrate.js`
- [CLI Configuration](https://github.com/rethinkdb/horizon/blob/next/cli/src/utils/config.js): `cli/src/utils/config.js`
- [Project Directory](https://github.com/rethinkdb/horizon/blob/next/cli/src/utils/change_to_project_dir.js): `cli/src/utils/change_to_project_dir.js`
- [Interrupt Handling](https://github.com/rethinkdb/horizon/blob/next/cli/src/utils/interrupt.js): `cli/src/utils/interrupt.js`
- [Local DB Launcher](https://github.com/rethinkdb/horizon/blob/next/cli/src/utils/start_rdb_server.js): `cli/src/utils/start_rdb_server.js`

### JavaScript Client

- [Client API](https://github.com/rethinkdb/horizon/blob/next/client/src/index.js): `client/src/index.js`
- [Query Builder](https://github.com/rethinkdb/horizon/blob/next/client/src/ast.js): `client/src/ast.js`
- [WebSocket Transport](https://github.com/rethinkdb/horizon/blob/next/client/src/socket.js): `client/src/socket.js`
- [Wire Serialization](https://github.com/rethinkdb/horizon/blob/next/client/src/serialization.js): `client/src/serialization.js`
- [Client Authentication](https://github.com/rethinkdb/horizon/blob/next/client/src/auth.js): `client/src/auth.js`
- [Client Models](https://github.com/rethinkdb/horizon/blob/next/client/src/model.js): `client/src/model.js`

### Horizon Server

- [HTTP And WS Server](https://github.com/rethinkdb/horizon/blob/next/server/src/server.js): `server/src/server.js`
- [Protocol Client](https://github.com/rethinkdb/horizon/blob/next/server/src/client.js): `server/src/client.js`
- [Request Lifecycle](https://github.com/rethinkdb/horizon/blob/next/server/src/request.js): `server/src/request.js`
- [Protocol Schemas](https://github.com/rethinkdb/horizon/blob/next/server/src/schema/horizon_protocol.js): `server/src/schema/horizon_protocol.js`
- [Query Endpoint](https://github.com/rethinkdb/horizon/blob/next/server/src/endpoint/query.js): `server/src/endpoint/query.js`
- [Write Endpoints](https://github.com/rethinkdb/horizon/tree/next/server/src/endpoint): `server/src/endpoint`
- [Subscription Endpoint](https://github.com/rethinkdb/horizon/blob/next/server/src/endpoint/subscribe.js): `server/src/endpoint/subscribe.js`
- [Collection Metadata](https://github.com/rethinkdb/horizon/tree/next/server/src/metadata): `server/src/metadata`
- [RethinkDB Connection](https://github.com/rethinkdb/horizon/blob/next/server/src/reql_connection.js): `server/src/reql_connection.js`

### Identity And Access

- [Authentication Service](https://github.com/rethinkdb/horizon/blob/next/server/src/auth.js): `server/src/auth.js`
- [Provider Adapters](https://github.com/rethinkdb/horizon/tree/next/server/src/auth): `server/src/auth`
- [Permission Rules](https://github.com/rethinkdb/horizon/tree/next/server/src/permissions): `server/src/permissions`
- [User Identity](https://github.com/rethinkdb/horizon/blob/next/server/src/metadata/metadata.js): `server/src/metadata/metadata.js`

### Other components

- **Developer**
- **RethinkDB**
- **Auth Providers**

## Connections

- Developer → Command Router: runs command (evidence: `cli/src/main.js`)
- Command Router → Project Setup: dispatches (evidence: `cli/src/main.js`)
- Command Router → Serve App: dispatches (evidence: `cli/src/main.js`)
- Command Router → Schema Manager: dispatches (evidence: `cli/src/main.js`)
- Command Router → Database Migration: dispatches (evidence: `cli/src/main.js`)
- Serve App → CLI Configuration: loads settings (evidence: `cli/src/serve.js`)
- Serve App → Project Directory: changes directory (evidence: `cli/src/serve.js`)
- Serve App → Local DB Launcher: starts local DB (evidence: `cli/src/serve.js`)
- Serve App → HTTP And WS Server: starts server
- Schema Manager → CLI Configuration: loads settings (evidence: `cli/src/schema.js`)
- Schema Manager → Collection Metadata: uses metadata (evidence: `cli/src/schema.js`)
- Database Migration → Schema Manager: reuses schema save (evidence: `cli/src/migrate.js`)
- Database Migration → CLI Configuration: loads settings (evidence: `cli/src/migrate.js`)
- Database Migration → Project Directory: changes directory (evidence: `cli/src/migrate.js`)
- Database Migration → Local DB Launcher: starts local DB (evidence: `cli/src/migrate.js`)
- Developer → Client API: uses API (evidence: `README.md`)
- Client API → Query Builder: builds queries (evidence: `client/src/index.js`)
- Client API → WebSocket Transport: sends requests (evidence: `client/src/index.js`)
- Client API → Client Authentication: handles tokens (evidence: `client/src/index.js`)
- Client API → Client Models: exposes models (evidence: `client/src/index.js`)
- WebSocket Transport → Wire Serialization: serializes messages (evidence: `client/src/socket.js`)
- Client API → HTTP And WS Server: uses protocol (evidence: `README.md`)
- HTTP And WS Server → Protocol Client: creates clients (evidence: `server/src/server.js`)
- HTTP And WS Server → RethinkDB Connection: opens connection (evidence: `server/src/server.js`)
- HTTP And WS Server → Authentication Service: configures auth (evidence: `server/src/server.js`)
- HTTP And WS Server → Query Endpoint: registers endpoint (evidence: `server/src/server.js`)
- HTTP And WS Server → Write Endpoints: registers endpoints (evidence: `server/src/server.js`)
- HTTP And WS Server → Subscription Endpoint: registers endpoint (evidence: `server/src/server.js`)
- Protocol Client → Protocol Schemas: validates messages (evidence: `server/src/client.js`)
- Protocol Client → Request Lifecycle: creates requests (evidence: `server/src/client.js`)
- Query Endpoint → Protocol Schemas: validates query (evidence: `server/src/endpoint/query.js`)
- Query Endpoint → Collection Metadata: reads collections (evidence: `server/src/endpoint/query.js`)
- Query Endpoint → Permission Rules: checks rules
- Query Endpoint → RethinkDB: runs query (evidence: `server/src/endpoint/query.js`)
- Write Endpoints → RethinkDB: writes records
- Subscription Endpoint → Query Endpoint: reuses query
- RethinkDB Connection → RethinkDB: connects (evidence: `README.md`)
- Authentication Service → Provider Adapters: uses adapters (evidence: `server/src/auth.js`)
- Authentication Service → User Identity: manages accounts
- Provider Adapters → Auth Providers: integrates (evidence: `README.md`)
- Authentication Service → Permission Rules: authorizes access
