Trait trillium_channels::ChannelHandler

source ·
pub trait ChannelHandler: Sized + Send + Sync + 'static {
    // Required method
    fn join_channel<'life0, 'life1, 'async_trait>(
        &'life0 self,
        conn: ChannelConn<'life1>,
        event: ChannelEvent,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;

    // Provided methods
    fn connect<'life0, 'life1, 'async_trait>(
        &'life0 self,
        conn: ChannelConn<'life1>,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn leave_channel<'life0, 'life1, 'async_trait>(
        &'life0 self,
        conn: ChannelConn<'life1>,
        event: ChannelEvent,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn incoming_message<'life0, 'life1, 'async_trait>(
        &'life0 self,
        conn: ChannelConn<'life1>,
        event: ChannelEvent,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn disconnect<'life0, 'life1, 'async_trait>(
        &'life0 self,
        conn: ChannelConn<'life1>,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
}
Expand description

§Trait for you to implement in order to define a Channel.

§Example

This simple example represents a simple chat server that’s compatible with the phoenix chat example – see channels/examples/channels.rs in this repo for a runnable example.

The only behavior we need to implement:

  • allow users to join the lobby channel
  • broadcast to all users when a new user has joined the lobby
  • broadcast all messages sent to the lobby channel to all users subscribed to the lobby channel.
use trillium_channels::{channel, ChannelConn, ChannelEvent, ChannelHandler};

struct ChatChannel;
#[trillium::async_trait]
impl ChannelHandler for ChatChannel {
    async fn join_channel(&self, conn: ChannelConn<'_>, event: ChannelEvent) {
        match event.topic() {
            "rooms:lobby" => {
                conn.allow_join(&event, &()).await;
                conn.broadcast(("rooms:lobby", "user:entered"));
            }

            _ => {}
        }
    }

    async fn incoming_message(&self, conn: ChannelConn<'_>, event: ChannelEvent) {
        match (event.topic(), event.event()) {
            ("rooms:lobby", "new:msg") => conn.broadcast(event),
            _ => {}
        }
    }
}

// fn main() {
//     trillium_smol::run(channel(ChatChannel));
// }

Required Methods§

source

fn join_channel<'life0, 'life1, 'async_trait>( &'life0 self, conn: ChannelConn<'life1>, event: ChannelEvent, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

join_channel is called when a websocket client sends a phx_join event. There is no default implementation to ensure that you implement the appropriate access control logic for your application. If you want clients to be able to connect to any channel they request, use this definition:

async fn join_channel(&self, conn: ChannelConn<'_>, event: ChannelEvent) {
    conn.allow_join(&event, &()).await;
}

Provided Methods§

source

fn connect<'life0, 'life1, 'async_trait>( &'life0 self, conn: ChannelConn<'life1>, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

connect is called once when each websocket client is connected. The default implementation does nothing.

source

fn leave_channel<'life0, 'life1, 'async_trait>( &'life0 self, conn: ChannelConn<'life1>, event: ChannelEvent, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

leave_channel is called when a websocket client sends a phx_leave event. The default implementation is to allow the user to leave that channel.

source

fn incoming_message<'life0, 'life1, 'async_trait>( &'life0 self, conn: ChannelConn<'life1>, event: ChannelEvent, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

incoming_message is called once for each ChannelEvent sent from a client. The default implementation does nothing.

source

fn disconnect<'life0, 'life1, 'async_trait>( &'life0 self, conn: ChannelConn<'life1>, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

disconnect is called when the websocket client ceases to be connected, either gracefully or abruptly.

Object Safety§

This trait is not object safe.

Implementors§