Architecture
Segra uses a dual-process architecture to separate concerns and maximize performance:- Backend Process (C#/.NET): Built on OBS, handles recording, video processing, file operations, and system integration
- Frontend Process (React/TypeScript): Provides the user interface through a WebView
Communication Layer
The frontend and backend communicate through two distinct channels:WebSocket
Real-time bidirectional communication for state updates, progress notifications, and system events.
window.external
Frontend-to-backend command channel for user actions and settings changes.
Frontend to Backend
The frontend sends commands to the backend using thewindow.external.sendMessage() interface, wrapped by the sendMessageToBackend utility:
Frontend/src/Utils/MessageUtils.ts
Message Format
All messages sent to the backend follow this structure:The command/action to execute (e.g., “StartRecording”, “CreateClip”)
Optional parameters specific to the method being called
Example: Starting a Recording
Backend to Frontend
The backend sends real-time updates to the frontend via WebSocket onws://localhost:5000/.
WebSocket Connection
The frontend establishes a WebSocket connection using theWebSocketProvider context:
Frontend/src/Context/WebSocketContext.tsx
Message Format
All WebSocket messages from the backend follow this structure:The event type (e.g., “Settings”, “ClipProgress”, “ShowModal”)
The payload data specific to the method
Heartbeat Protocol
The WebSocket connection maintains a heartbeat to detect disconnections:- Frontend → Backend: Sends
"ping"every 15 seconds - Backend → Frontend: Responds with
{"method": "pong", "content": {}} - Timeout: Connection considered dead after 30 seconds without a pong
Connection Lifecycle
Initial Sync
Backend responds with:
- Current settings state (
Settingsmessage) - Available games list (
GameListmessage) - App version (
AppVersionmessage)
The backend automatically closes any existing WebSocket connection when a new one is established, ensuring only one active connection at a time.
State Management
The backend maintains application state through theSettings.Instance singleton (Backend/Core/Models/Settings.cs). Key state includes:
- Recording State: Active recordings, pre-recordings
- Content Library: Videos, clips, replays with metadata
- User Preferences: Video quality, storage locations, keybinds
- Game Lists: Whitelisted and blacklisted games
Settings message to sync the frontend:
Backend/App/MessageService.cs
Error Handling
Both communication channels implement error handling:Frontend Errors
Backend Errors
The backend catches and logs exceptions in the message handler:ShowModal message: