Skip to main content

pg_queue/
notify.rs

1use crate::errors::Result;
2use sqlx::PgPool;
3
4/// Service for sending PostgreSQL NOTIFY messages.
5///
6/// Note: PostgreSQL NOTIFY has an 8KB payload limit.
7#[derive(Clone)]
8pub struct NotifyService {
9    pool: PgPool,
10}
11
12impl NotifyService {
13    pub fn new(pool: PgPool) -> Self {
14        Self { pool }
15    }
16
17    /// Send a notification to a channel
18    pub async fn notify(&self, channel: &str, payload: &str) -> Result<()> {
19        sqlx::query("SELECT pg_notify($1, $2)")
20            .bind(channel)
21            .bind(payload)
22            .execute(&self.pool)
23            .await?;
24
25        Ok(())
26    }
27}
28
29#[cfg(test)]
30mod tests {
31    #[test]
32    fn test_channel_formatting() {
33        let channel = format!("events.{}", "user_123");
34        assert_eq!(channel, "events.user_123");
35    }
36}