|
| 1 | +# Architecture Overview |
| 2 | + |
| 3 | +This architecture showcases different ways for clients to interact with a **Hocus Pocus Server** (a [Y.js](https://github.com/yjs/yjs) provider) through either WebSockets, HTTP fallbacks, or Server-Sent Events (SSE) when WebSockets are not available. |
| 4 | + |
| 5 | +**Main Components**: |
| 6 | + |
| 7 | +- **Client**: The front-end application or user agent. |
| 8 | +- **Nginx**: A reverse proxy handling incoming requests, forwarding them to the appropriate services, and managing SSL/TLS termination if needed. |
| 9 | +- **Auth Sub Request (Django)**: Handles authentication/authorization, ensuring requests have valid credentials or permissions. |
| 10 | +- **Hocus Pocus Server**: The core collaborative editing server (powered by [Y.js](https://github.com/yjs/yjs) libraries) that manages document state and synchronization. |
| 11 | +- **Express**: Fallback server to handle push or pull requests when WebSocket connections fail. |
| 12 | +- **SSE**: A mechanism (Server-Sent Events) for real-time updates when WebSockets are unavailable. |
| 13 | + |
| 14 | +## Mermaid Diagram |
| 15 | + |
| 16 | +```mermaid |
| 17 | +flowchart TD |
| 18 | + title1[WebSocket Success]-->Client1(Client)<--->|WebSocket Success|WS1(Websocket) --> Nginx1(Ngnix) <--> Auth1("Auth Sub Request (Django)") --->|With the good right|YServer1("Hocus Pocus Server") |
| 19 | + YServer1 --> WS1 |
| 20 | + YServer1 <--> clients(Dispatch to clients) |
| 21 | +
|
| 22 | + title2[WebSocket Fails - Push data]-->Client2(Client)---|WebSocket fails|HTTP2(HTTP) --> Nginx2(Ngnix) <--> Auth2("Auth Sub Request (Django)")--->|With the good right|Express2(Express) --> YServer2("Hocus Pocus Server") --> clients(Dispatch to clients) |
| 23 | +
|
| 24 | + title3[WebSocket Fails - Pull data]-->Client3(Client)<--->|WebSocket fails|SSE(SSE) --> Nginx3(Ngnix) <--> Auth3("Auth Sub Request (Django)") --->|With the good right|Express3(Express) --> YServer3("Listen Hocus Pocus Server") |
| 25 | + YServer3("Listen Hocus Pocus Server") --> SSE |
| 26 | + YServer3("Listen Hocus Pocus Server") <--> clients(Data from clients) |
| 27 | +``` |
| 28 | + |
| 29 | +--- |
| 30 | + |
| 31 | +## Detailed Flows |
| 32 | + |
| 33 | +### 1. WebSocket Success |
| 34 | +1. **Client** attempts a WebSocket connection. |
| 35 | +2. **Nginx** proxies the WebSocket connection through the **Auth Sub Request (Django)** for authentication. |
| 36 | +3. Once authenticated, traffic is routed to the **Hocus Pocus Server**. |
| 37 | +4. The server can broadcast data to all clients connected through WebSockets. |
| 38 | + - Note: The path `YServer1 --> WS1` indicates the two-way real-time communication between the server and client(s). |
| 39 | + |
| 40 | +### 2. WebSocket Fails — Push Data (HTTP) |
| 41 | +If WebSocket connections fail, clients can **push** data via HTTP: |
| 42 | +1. **Client** detects WebSocket failure and falls back to sending data over **HTTP**. |
| 43 | +2. **Nginx** handles HTTP requests and authenticates them via the **Auth Sub Request (Django)**. |
| 44 | +3. After successful authentication, the requests go to an **Express** server. |
| 45 | +4. The **Express** server relays changes to the **Hocus Pocus Server**. |
| 46 | +5. The **Hocus Pocus Server** dispatches updated content to connected clients. |
| 47 | + |
| 48 | +### 3. WebSocket Fails — Pull Data (SSE) |
| 49 | +For continuously receiving data when WebSockets fail, the client can **pull** data using SSE: |
| 50 | +1. **Client** sets up an **SSE** connection. |
| 51 | +2. **Nginx** proxies the SSE stream request through the **Auth Sub Request (Django)** for authentication. |
| 52 | +3. Once authenticated, the **Express** server listens to the **Hocus Pocus Server** for changes. |
| 53 | +4. The server then sends updates back to the **Client** through SSE in near real-time. |
| 54 | + |
| 55 | +--- |
| 56 | + |
| 57 | +## Component Responsibilities |
| 58 | + |
| 59 | +| **Component** | **Responsibility** | |
| 60 | +|-----------------------------|-----------------------------------------------------------------------------------------| |
| 61 | +| **Client** | Initiates connections (WebSocket/HTTP/SSE), displays and interacts with data | |
| 62 | +| **Nginx** | Acts as a reverse proxy, routes traffic, handles SSL, and passes auth sub requests | |
| 63 | +| **Auth Sub Request (Django)** | Validates requests, ensuring correct permissions and tokens | |
| 64 | +| **WebSocket** | Real-time two-way communication channel | |
| 65 | +| **HTTP** | Fallback method for sending updates when WebSockets are not available | |
| 66 | +| **Express** | Fallback server for handling requests (push/pull of data) | |
| 67 | +| **SSE** | Mechanism for real-time one-way updates from server to client | |
| 68 | +| **Hocus Pocus Server** | Core Y.js server for collaboration, managing document states and synchronization | |
| 69 | + |
| 70 | +--- |
| 71 | + |
| 72 | +## Why This Setup? |
| 73 | + |
| 74 | +- **Reliability:** Ensures that when a user’s browser or network environment does not support WebSockets, there are fallback mechanisms (HTTP for push updates and SSE for server-initiated updates). |
| 75 | +- **Scalability:** Nginx can efficiently proxy requests and scale horizontally, while the authentication step is centralized in Django. |
| 76 | +- **Security:** The Auth Sub Request in Django enforces proper permissions before data is relayed to the collaboration server. |
| 77 | +- **Real-time Collaboration:** The Hocus Pocus Server provides low-latency updates, essential for collaborative editing, supported by [Y.js](https://github.com/yjs/yjs). |
| 78 | + |
| 79 | +--- |
| 80 | + |
| 81 | +### Contributing |
| 82 | +If you have any suggestions or improvements, feel free to open an issue or submit a pull request. |
| 83 | + |
| 84 | +**Thank you for exploring this architecture!** If you have any questions or need more detailed explanations, please let us know. |
0 commit comments