Real-time State Streaming
Signal.Engine utilizes a reactive architecture to ensure that the "Brain's" decisions and the simulation's state are synchronized with the frontend dashboard in real-time. This is achieved through a combination of asynchronous background tasks and a WebSocket broadcast system.
Real-time State Streaming
The system provides a low-latency pipeline to stream market regimes, agent decisions, and portfolio updates. This allows users to monitor the Generative Agent's "thought process" as it happens.
WebSocket Interface
The backend broadcasts updates via a dedicated WebSocket connection. This is the primary method for the dashboard to receive "push" updates without manual refreshing.
- Endpoint:
ws://<host>:8000/ws - Protocol: JSON-encoded packets
Event: scan_complete
This event is broadcasted whenever the Hybrid Brain finishes processing a market sequence and the Simulation Engine updates the portfolio state.
Payload Structure:
{
"type": "scan_complete",
"data": [
{
"Ticker": "RELIANCE.NS",
"Action": "BUY",
"Confidence": 0.89,
"Rational": ["Strong Momentum", "Low Volatility Regime"]
}
],
"simulation": {
"cash": 95400.0,
"equity": 5200.0,
"pnl": 600.0,
"status": "ALIVE"
},
"logs": ["Brain started thinking...", "✅ Brain finished thinking."]
}
Background Processing Lifecycle
To prevent blocking the API, market analysis is handled as a non-blocking background task.
- Trigger: An external request to
/api/scanor an automated internal timer triggers thebackground_scan. - Inference: The
HybridBrainprocesses sequence data (LSTM) and evaluates risk. - Simulation: The
SimulationEngineprocesses the resulting "tick" to update the virtual paper trading account. - Broadcast: The final state is pushed to all connected WebSocket clients via
broadcast_update.
REST API Fallbacks
While WebSockets are preferred for streaming, the following endpoints provide the current state of the system for polling or manual inspection:
| Endpoint | Method | Description |
| :--- | :--- | :--- |
| /api/scan | GET | Triggers a new background analysis. Returns busy if a scan is already in progress. |
| /api/results | GET | Fetches the most recent decisions and simulation metrics. |
| /api/simulation/state | GET | Returns the current portfolio balance, PnL, and status. |
Frontend Integration
The React dashboard consumes this stream to provide real-time visual feedback. High-confidence signals (Confidence > 85%) are automatically passed to the notificationService to trigger system-level alerts.
Example: Consuming the Scan Loop
// From App.tsx
useEffect(() => {
const interval = setInterval(() => {
// Triggers the background scan every 2 seconds if auto-mode is enabled
if (isAuto && !loading) {
axios.get('/api/scan');
}
}, 2000);
return () => clearInterval(interval);
}, [isAuto, loading]);
Rate Limiting & Performance
The streaming API includes a built-in RateLimitMiddleware to ensure system stability during high-frequency data ingestion:
- Default Limit: 120 calls per minute per IP.
- Safety Lock: The
IS_SCANNINGflag prevents the Brain from starting a new inference cycle if the previous one has not yet completed.