|
5 | 5 | namespace PerfProblemSimulator.Hubs; |
6 | 6 |
|
7 | 7 | /// <summary> |
8 | | -/// SignalR hub for real-time metrics broadcasting to dashboard clients. |
| 8 | +/// WebSocket/SignalR hub for real-time metrics broadcasting to dashboard clients. |
9 | 9 | /// </summary> |
10 | 10 | /// <remarks> |
11 | 11 | /// <para> |
12 | | -/// <strong>Educational Note:</strong> |
| 12 | +/// <strong>PURPOSE:</strong> |
| 13 | +/// Provides a persistent bidirectional communication channel between the server and |
| 14 | +/// connected browser clients. This enables real-time dashboard updates without polling. |
13 | 15 | /// </para> |
14 | 16 | /// <para> |
15 | | -/// SignalR provides real-time communication between server and browser clients. |
16 | | -/// This hub pushes metrics to all connected dashboard instances automatically. |
| 17 | +/// <strong>ALGORITHM:</strong> |
| 18 | +/// <list type="number"> |
| 19 | +/// <item>Browser connects to /hubs/metrics endpoint (WebSocket preferred, with fallbacks)</item> |
| 20 | +/// <item>On connect: immediately send current metrics so client doesn't wait for next tick</item> |
| 21 | +/// <item>Every 1 second: MetricsBroadcastService pushes metrics to all connected clients</item> |
| 22 | +/// <item>On simulation events: push notifications so UI can update status indicators</item> |
| 23 | +/// <item>On disconnect: clean up resources (automatic via framework)</item> |
| 24 | +/// </list> |
17 | 25 | /// </para> |
18 | 26 | /// <para> |
19 | | -/// <strong>How it works:</strong> |
20 | | -/// </para> |
21 | | -/// <list type="number"> |
22 | | -/// <item>Browser connects to /hubs/metrics via WebSocket (with fallback)</item> |
23 | | -/// <item>MetricsCollector fires MetricsCollected event every second</item> |
24 | | -/// <item>This hub broadcasts the snapshot to all connected clients</item> |
25 | | -/// <item>Browser JavaScript receives the data and updates charts</item> |
| 27 | +/// <strong>WHY REAL-TIME vs POLLING:</strong> |
| 28 | +/// <list type="bullet"> |
| 29 | +/// <item>Sub-second updates are essential for visualizing performance problems as they happen</item> |
| 30 | +/// <item>Push-based is more efficient than clients polling every second</item> |
| 31 | +/// <item>Connection state enables cleanup when browsers close</item> |
| 32 | +/// <item>Transport fallback (WebSocket → SSE → Long Polling) ensures broad compatibility</item> |
26 | 33 | /// </list> |
| 34 | +/// </para> |
27 | 35 | /// <para> |
28 | | -/// <strong>Why SignalR over polling?</strong> |
| 36 | +/// <strong>PORTING TO OTHER LANGUAGES:</strong> |
| 37 | +/// <list type="bullet"> |
| 38 | +/// <item>PHP: Use Ratchet or ReactPHP for WebSocket server, or external service like Pusher</item> |
| 39 | +/// <item>Node.js: Use Socket.IO - nearly identical concept with io.on('connection', socket => ...)</item> |
| 40 | +/// <item>Java/Spring: Use @MessageMapping with SimpMessagingTemplate for WebSocket STOMP</item> |
| 41 | +/// <item>Python: Use Flask-SocketIO with @socketio.on('connect') handlers</item> |
| 42 | +/// <item>Ruby: Use ActionCable with channel subscriptions</item> |
| 43 | +/// </list> |
29 | 44 | /// </para> |
| 45 | +/// <para> |
| 46 | +/// <strong>RELATED FILES:</strong> |
30 | 47 | /// <list type="bullet"> |
31 | | -/// <item> |
32 | | -/// <term>Efficiency</term> |
33 | | -/// <description>Single connection, push-based updates</description> |
34 | | -/// </item> |
35 | | -/// <item> |
36 | | -/// <term>Real-time</term> |
37 | | -/// <description>Sub-second latency vs polling intervals</description> |
38 | | -/// </item> |
39 | | -/// <item> |
40 | | -/// <term>Scalability</term> |
41 | | -/// <description>SignalR handles connection management</description> |
42 | | -/// </item> |
43 | | -/// <item> |
44 | | -/// <term>Transport flexibility</term> |
45 | | -/// <description>WebSocket → Server-Sent Events → Long Polling fallback</description> |
46 | | -/// </item> |
| 48 | +/// <item>Hubs/IMetricsClient.cs - Message contract (what can be sent to clients)</item> |
| 49 | +/// <item>Services/MetricsBroadcastService.cs - Background service that triggers broadcasts</item> |
| 50 | +/// <item>wwwroot/js/dashboard.js - JavaScript SignalR client connection</item> |
| 51 | +/// <item>Program.cs - Hub endpoint mapping (/hubs/metrics)</item> |
47 | 52 | /// </list> |
| 53 | +/// </para> |
48 | 54 | /// </remarks> |
49 | 55 | public class MetricsHub : Hub<IMetricsClient> |
50 | 56 | { |
|
0 commit comments