bext-realtime 0.2.0

Realtime pub/sub for bext — WebSocket and SSE with optional Redis relay
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
//! WebSocket session management: handles ping/pong heartbeats, JSON-framed
//! client messages (subscribe, unsubscribe, publish), and server push delivery.

use std::sync::Arc;
use std::time::{Duration, Instant};

use parking_lot::Mutex;
use serde_json::Value;
use tokio::sync::mpsc;
use tracing::debug;

use crate::hub::BextHub;
use crate::message::{ClientMessage, HubEvent, ServerMessage};

/// Configuration for a WebSocket session.
#[derive(Debug, Clone)]
pub struct WsSessionConfig {
    /// How often to send Ping frames.
    pub heartbeat_interval: Duration,
    /// How long to wait for a Pong before considering the connection dead.
    pub pong_timeout: Duration,
}

impl Default for WsSessionConfig {
    fn default() -> Self {
        Self {
            heartbeat_interval: Duration::from_secs(30),
            pong_timeout: Duration::from_secs(10),
        }
    }
}

/// Manages one WebSocket connection's lifecycle and message routing.
///
/// This struct doesn't own the WebSocket transport directly — it provides
/// the logic layer. The transport integration (e.g. actix-web, tungstenite)
/// calls into `WsSession` methods.
pub struct WsSession {
    /// Shared hub reference.
    hub: Arc<BextHub>,
    /// This session's subscriber ID in the hub (set after first subscribe).
    subscriber_id: Option<u64>,
    /// Receiver for hub events routed to this subscriber (bounded).
    hub_receiver: Option<mpsc::Receiver<HubEvent>>,
    /// Outbound message queue (read by the transport layer, bounded).
    outbound: mpsc::Sender<ServerMessage>,
    /// Outbound receiver (consumed by the transport layer).
    outbound_rx: Option<mpsc::Receiver<ServerMessage>>,
    /// Last time we received a Pong.
    last_pong: Arc<Mutex<Instant>>,
    /// Configuration.
    config: WsSessionConfig,
}

impl WsSession {
    /// Create a new WebSocket session.
    ///
    /// Call `take_outbound_receiver()` to get the stream of messages to send
    /// to the WebSocket client.
    pub fn new(hub: Arc<BextHub>, config: WsSessionConfig) -> Self {
        let (outbound_tx, outbound_rx) = mpsc::channel(256);
        Self {
            hub,
            subscriber_id: None,
            hub_receiver: None,
            outbound: outbound_tx,
            outbound_rx: Some(outbound_rx),
            last_pong: Arc::new(Mutex::new(Instant::now())),
            config,
        }
    }

    /// Take the outbound message receiver.
    ///
    /// The transport layer reads from this to send messages over the WebSocket.
    /// Can only be called once.
    pub fn take_outbound_receiver(&mut self) -> Option<mpsc::Receiver<ServerMessage>> {
        self.outbound_rx.take()
    }

    /// Take the hub event receiver.
    ///
    /// The transport layer reads from this and calls `forward_hub_event` for each.
    /// Can only be called once (after at least one subscribe).
    pub fn take_hub_receiver(&mut self) -> Option<mpsc::Receiver<HubEvent>> {
        self.hub_receiver.take()
    }

    /// Handle an incoming text message from the WebSocket client.
    ///
    /// Parses JSON into `ClientMessage` and dispatches accordingly.
    /// Returns an error string if parsing fails.
    pub fn handle_text(&mut self, text: &str) -> Result<(), String> {
        let msg: ClientMessage =
            serde_json::from_str(text).map_err(|e| format!("invalid message: {}", e))?;
        self.handle_message(msg);
        Ok(())
    }

    /// Handle a parsed `ClientMessage`.
    pub fn handle_message(&mut self, msg: ClientMessage) {
        match msg {
            ClientMessage::Subscribe { topics } => self.handle_subscribe(topics),
            ClientMessage::Unsubscribe { topics } => self.handle_unsubscribe(topics),
            ClientMessage::Publish { topic, data } => self.handle_publish(topic, data),
            ClientMessage::Pong => self.handle_pong(),
        }
    }

    /// Forward a hub event to the WebSocket client as a `ServerMessage::Event`.
    pub fn forward_hub_event(&self, event: HubEvent) {
        let msg = ServerMessage::Event {
            topic: event.topic,
            data: event.data,
            id: event.id,
        };
        let _ = self.outbound.try_send(msg);
    }

    /// Send a Ping to the client. Called periodically by the transport layer.
    pub fn send_ping(&self) {
        let _ = self.outbound.try_send(ServerMessage::Ping);
    }

    /// Check if the connection is alive (received a pong within timeout).
    pub fn is_alive(&self) -> bool {
        let last = *self.last_pong.lock();
        last.elapsed() < self.config.heartbeat_interval + self.config.pong_timeout
    }

    /// Send an error message to the client.
    pub fn send_error(&self, message: String) {
        let _ = self.outbound.try_send(ServerMessage::Error { message });
    }

    /// Get the subscriber ID (if subscribed).
    pub fn subscriber_id(&self) -> Option<u64> {
        self.subscriber_id
    }

    /// Get the configuration.
    pub fn config(&self) -> &WsSessionConfig {
        &self.config
    }

    /// Clean up on disconnect — unsubscribe from the hub.
    pub fn cleanup(&mut self) {
        if let Some(id) = self.subscriber_id.take() {
            self.hub.unsubscribe(id);
            debug!(subscriber_id = id, "ws session cleaned up");
        }
    }

    // ── Private handlers ────────────────────────────────────────────

    fn handle_subscribe(&mut self, topics: Vec<String>) {
        if topics.is_empty() {
            self.send_error("subscribe: topics list is empty".to_string());
            return;
        }

        if let Some(id) = self.subscriber_id {
            // Already subscribed — add more topics
            self.hub.add_topics(id, topics.clone());
        } else {
            // First subscription — register with hub
            match self.hub.subscribe(topics.clone()) {
                Some((id, rx)) => {
                    self.subscriber_id = Some(id);
                    self.hub_receiver = Some(rx);
                    debug!(subscriber_id = id, "ws client subscribed");
                }
                None => {
                    self.send_error("max connections reached".to_string());
                    return;
                }
            }
        }

        let _ = self.outbound.try_send(ServerMessage::Subscribed { topics });
    }

    fn handle_unsubscribe(&mut self, topics: Vec<String>) {
        if let Some(id) = self.subscriber_id {
            self.hub.remove_topics(id, topics);
        }
    }

    fn handle_publish(&self, topic: String, data: Value) {
        self.hub.publish(&topic, data);
    }

    fn handle_pong(&self) {
        let mut last = self.last_pong.lock();
        *last = Instant::now();
    }
}

impl Drop for WsSession {
    fn drop(&mut self) {
        self.cleanup();
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::hub::{BextHub, HubConfig};
    use serde_json::json;
    use std::sync::Arc;

    fn test_hub() -> Arc<BextHub> {
        Arc::new(BextHub::new(HubConfig::default()))
    }

    fn test_session(hub: Arc<BextHub>) -> WsSession {
        WsSession::new(hub, WsSessionConfig::default())
    }

    // ── Message parsing ─────────────────────────────────────────────

    #[test]
    fn handle_text_valid_subscribe() {
        let hub = test_hub();
        let mut session = test_session(hub);
        let result = session.handle_text(r#"{"type":"subscribe","topics":["app/events"]}"#);
        assert!(result.is_ok());
        assert!(session.subscriber_id().is_some());
    }

    #[test]
    fn handle_text_valid_pong() {
        let hub = test_hub();
        let mut session = test_session(hub);
        let result = session.handle_text(r#"{"type":"pong"}"#);
        assert!(result.is_ok());
    }

    #[test]
    fn handle_text_invalid_json() {
        let hub = test_hub();
        let mut session = test_session(hub);
        let result = session.handle_text("not json");
        assert!(result.is_err());
    }

    #[test]
    fn handle_text_unknown_type() {
        let hub = test_hub();
        let mut session = test_session(hub);
        let result = session.handle_text(r#"{"type":"unknown"}"#);
        assert!(result.is_err());
    }

    // ── Subscribe flow ──────────────────────────────────────────────

    #[test]
    fn subscribe_creates_subscriber() {
        let hub = test_hub();
        let mut session = test_session(hub.clone());
        let mut outbound = session.take_outbound_receiver().unwrap();

        session.handle_message(ClientMessage::Subscribe {
            topics: vec!["test".to_string()],
        });

        assert!(session.subscriber_id().is_some());
        assert_eq!(hub.subscriber_count(), 1);

        // Should receive Subscribed confirmation
        let msg = outbound.try_recv().unwrap();
        match msg {
            ServerMessage::Subscribed { topics } => {
                assert_eq!(topics, vec!["test".to_string()]);
            }
            other => panic!("expected Subscribed, got {:?}", other),
        }
    }

    #[test]
    fn subscribe_empty_topics_sends_error() {
        let hub = test_hub();
        let mut session = test_session(hub);
        let mut outbound = session.take_outbound_receiver().unwrap();

        session.handle_message(ClientMessage::Subscribe { topics: vec![] });

        assert!(session.subscriber_id().is_none());

        let msg = outbound.try_recv().unwrap();
        match msg {
            ServerMessage::Error { message } => {
                assert!(message.contains("empty"));
            }
            other => panic!("expected Error, got {:?}", other),
        }
    }

    #[test]
    fn subscribe_twice_adds_topics() {
        let hub = test_hub();
        let mut session = test_session(hub.clone());
        let _outbound = session.take_outbound_receiver().unwrap();

        session.handle_message(ClientMessage::Subscribe {
            topics: vec!["a".to_string()],
        });
        let first_id = session.subscriber_id().unwrap();

        session.handle_message(ClientMessage::Subscribe {
            topics: vec!["b".to_string()],
        });
        // Should keep the same subscriber ID
        assert_eq!(session.subscriber_id().unwrap(), first_id);
        // Hub should have 2 topics
        assert_eq!(hub.topic_count(), 2);
    }

    // ── Unsubscribe flow ────────────────────────────────────────────

    #[test]
    fn unsubscribe_removes_topics() {
        let hub = test_hub();
        let mut session = test_session(hub.clone());
        let _outbound = session.take_outbound_receiver().unwrap();

        session.handle_message(ClientMessage::Subscribe {
            topics: vec!["a".to_string(), "b".to_string()],
        });
        assert_eq!(hub.topic_count(), 2);

        session.handle_message(ClientMessage::Unsubscribe {
            topics: vec!["a".to_string()],
        });
        assert_eq!(hub.topic_count(), 1);
    }

    #[test]
    fn unsubscribe_without_subscribe_is_noop() {
        let hub = test_hub();
        let mut session = test_session(hub);
        session.handle_message(ClientMessage::Unsubscribe {
            topics: vec!["a".to_string()],
        });
        // Should not panic
    }

    // ── Publish flow ────────────────────────────────────────────────

    #[tokio::test]
    async fn publish_from_ws_delivers_to_other_subscribers() {
        let hub = test_hub();
        let mut session = test_session(hub.clone());
        let _outbound = session.take_outbound_receiver().unwrap();

        // Another subscriber listens
        let (_id, mut rx) = hub.subscribe(vec!["chat".to_string()]).unwrap();

        // Publish via ws session
        session.handle_message(ClientMessage::Publish {
            topic: "chat".to_string(),
            data: json!({"text": "hello"}),
        });

        let evt = rx.recv().await.unwrap();
        assert_eq!(evt.topic, "chat");
        assert_eq!(evt.data, json!({"text": "hello"}));
    }

    // ── Pong / liveness ─────────────────────────────────────────────

    #[test]
    fn pong_updates_last_pong_time() {
        let hub = test_hub();
        let mut session = test_session(hub);

        // Set last_pong to the past
        {
            let mut last = session.last_pong.lock();
            *last = Instant::now() - Duration::from_secs(100);
        }

        assert!(!session.is_alive());

        session.handle_message(ClientMessage::Pong);
        assert!(session.is_alive());
    }

    #[test]
    fn is_alive_true_initially() {
        let hub = test_hub();
        let session = test_session(hub);
        assert!(session.is_alive());
    }

    // ── Ping ────────────────────────────────────────────────────────

    #[test]
    fn send_ping_queues_ping_message() {
        let hub = test_hub();
        let mut session = test_session(hub);
        let mut outbound = session.take_outbound_receiver().unwrap();

        session.send_ping();

        let msg = outbound.try_recv().unwrap();
        assert_eq!(msg, ServerMessage::Ping);
    }

    // ── Forward hub event ───────────────────────────────────────────

    #[test]
    fn forward_hub_event_sends_event_message() {
        let hub = test_hub();
        let mut session = test_session(hub);
        let mut outbound = session.take_outbound_receiver().unwrap();

        let event = HubEvent {
            id: 5,
            topic: "test".to_string(),
            data: json!({"key": "val"}),
            timestamp: chrono::Utc::now(),
        };
        session.forward_hub_event(event);

        let msg = outbound.try_recv().unwrap();
        match msg {
            ServerMessage::Event { topic, data, id } => {
                assert_eq!(topic, "test");
                assert_eq!(data, json!({"key": "val"}));
                assert_eq!(id, 5);
            }
            other => panic!("expected Event, got {:?}", other),
        }
    }

    // ── Cleanup / Drop ──────────────────────────────────────────────

    #[test]
    fn cleanup_unsubscribes_from_hub() {
        let hub = test_hub();
        let mut session = test_session(hub.clone());
        let _outbound = session.take_outbound_receiver().unwrap();

        session.handle_message(ClientMessage::Subscribe {
            topics: vec!["a".to_string()],
        });
        assert_eq!(hub.subscriber_count(), 1);

        session.cleanup();
        assert_eq!(hub.subscriber_count(), 0);
        assert!(session.subscriber_id().is_none());
    }

    #[test]
    fn drop_triggers_cleanup() {
        let hub = test_hub();
        {
            let mut session = test_session(hub.clone());
            let _outbound = session.take_outbound_receiver().unwrap();

            session.handle_message(ClientMessage::Subscribe {
                topics: vec!["a".to_string()],
            });
            assert_eq!(hub.subscriber_count(), 1);
        } // session dropped here

        assert_eq!(hub.subscriber_count(), 0);
    }

    // ── Max connections via WS ──────────────────────────────────────

    #[test]
    fn subscribe_at_max_connections_sends_error() {
        let hub = Arc::new(BextHub::new(HubConfig {
            max_connections: 1,
            ..Default::default()
        }));

        // First session succeeds
        let mut s1 = test_session(hub.clone());
        let _out1 = s1.take_outbound_receiver().unwrap();
        s1.handle_message(ClientMessage::Subscribe {
            topics: vec!["a".to_string()],
        });
        assert!(s1.subscriber_id().is_some());

        // Second session fails
        let mut s2 = test_session(hub.clone());
        let mut out2 = s2.take_outbound_receiver().unwrap();
        s2.handle_message(ClientMessage::Subscribe {
            topics: vec!["b".to_string()],
        });
        assert!(s2.subscriber_id().is_none());

        let msg = out2.try_recv().unwrap();
        match msg {
            ServerMessage::Error { message } => {
                assert!(message.contains("max connections"));
            }
            other => panic!("expected Error, got {:?}", other),
        }
    }

    // ── Send error ──────────────────────────────────────────────────

    #[test]
    fn send_error_queues_error_message() {
        let hub = test_hub();
        let mut session = test_session(hub);
        let mut outbound = session.take_outbound_receiver().unwrap();

        session.send_error("test error".to_string());

        let msg = outbound.try_recv().unwrap();
        match msg {
            ServerMessage::Error { message } => {
                assert_eq!(message, "test error");
            }
            other => panic!("expected Error, got {:?}", other),
        }
    }
}