# Braid Middleware Design **Goal**: Implement HTTP Keep-Alive mechanism for Braid updates to support real-time streams. ## Current State `Network/Wai/Middleware/Braid.hs` implements the Braid protocol headers but lacks a robust mechanism to keep the connection open and push updates. ## Design Requirements 1. **Connection Management**: - Identify Braid subscriptions via headers. - Keep the response body open (streaming response). - Handle client disconnects gracefully. 2. **Update Channel**: - Use a `TChan` or `BroadcastChan` to signal updates to the connection handler. - When a resource changes, push a new Braid frame to the open stream. 3. **Frame Format**: - Adhere to Braid spec for patch frames. - `Content-Type: application/json` (or relevant type). - `Merge-Type: braid`. ## Implementation Plan 1. Modify middleware to hijack response for Braid requests. 2. Spawn a lightweight thread to listen on an update channel. 3. Stream chunks to the client.