supabase_realtime_rs/types/
message.rs

1use serde::{Deserialize, Serialize};
2
3use crate::ChannelEvent;
4
5#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
6pub struct RealtimeMessage {
7    pub topic: String,
8    pub event: ChannelEvent,
9    #[serde(default)]
10    pub payload: serde_json::Value,
11    #[serde(skip_serializing_if = "Option::is_none")]
12    pub r#ref: Option<String>,
13    #[serde(skip_serializing_if = "Option::is_none")]
14    pub join_ref: Option<String>,
15}
16
17impl RealtimeMessage {
18    pub fn new(topic: String, event: ChannelEvent, payload: serde_json::Value) -> Self {
19        Self {
20            topic,
21            event,
22            payload,
23            r#ref: None,
24            join_ref: None,
25        }
26    }
27
28    pub fn with_ref(mut self, r#ref: String) -> Self {
29        self.r#ref = Some(r#ref);
30        self
31    }
32
33    pub fn with_join_ref(mut self, join_ref: String) -> Self {
34        self.join_ref = Some(join_ref);
35        self
36    }
37}
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42
43    #[test]
44    fn test_realtime_message() {
45        let message = RealtimeMessage::new(
46            "test".to_string(),
47            ChannelEvent::Custom("message".to_string()),
48            serde_json::Value::Null,
49        );
50        assert_eq!(message.topic, "test");
51        assert_eq!(message.event, ChannelEvent::Custom("message".to_string()));
52        assert_eq!(message.payload, serde_json::Value::Null);
53        assert_eq!(message.r#ref, None);
54        assert_eq!(message.join_ref, None);
55    }
56
57    #[test]
58    fn test_realtime_message_round_trip() {
59        let message = RealtimeMessage::new(
60            "test".to_string(),
61            ChannelEvent::Custom("message".to_string()),
62            serde_json::Value::Null,
63        )
64        .with_ref("1".to_string())
65        .with_join_ref("321".to_string());
66
67        let serialized = serde_json::to_string(&message).unwrap();
68        let deserialized: RealtimeMessage = serde_json::from_str(&serialized).unwrap();
69
70        assert_eq!(message, deserialized);
71    }
72
73    #[test]
74    fn test_realtime_message_serialization_without_ref_and_join_ref() {
75        let message = RealtimeMessage::new(
76            "test".to_string(),
77            ChannelEvent::Custom("message".to_string()),
78            serde_json::Value::Null,
79        );
80
81        let json = serde_json::to_string(&message).unwrap();
82        assert!(!json.contains(r#""ref":"#));
83        assert!(!json.contains(r#""join_ref":"#));
84    }
85
86    #[test]
87    fn test_realtime_message_serialization_with_ref_and_join_ref() {
88        let message = RealtimeMessage::new(
89            "test".to_string(),
90            ChannelEvent::Custom("message".to_string()),
91            serde_json::Value::Null,
92        )
93        .with_ref("123".to_string())
94        .with_join_ref("321".to_string());
95
96        let json = serde_json::to_string(&message).unwrap();
97        assert!(json.contains(r#""ref":"123""#));
98        assert!(json.contains(r#""join_ref":"321""#));
99    }
100}