trillium_channels/channel_handler.rs
1use crate::{ChannelConn, ChannelEvent};
2
3/**
4# Trait for you to implement in order to define a [`Channel`](crate::Channel).
5
6## Example
7
8This simple example represents a simple chat server that's
9compatible with the [phoenix chat
10example](https://github.com/chrismccord/phoenix_chat_example) -- see
11channels/examples/channels.rs in this repo for a runnable example.
12
13The only behavior we need to implement:
14
15* allow users to join the lobby channel
16* broadcast to all users when a new user has joined the lobby
17* broadcast all messages sent to the lobby channel to all users
18 subscribed to the lobby channel.
19
20```
21use trillium_channels::{channel, ChannelConn, ChannelEvent, ChannelHandler};
22
23struct ChatChannel;
24#[trillium::async_trait]
25impl ChannelHandler for ChatChannel {
26 async fn join_channel(&self, conn: ChannelConn<'_>, event: ChannelEvent) {
27 match event.topic() {
28 "rooms:lobby" => {
29 conn.allow_join(&event, &()).await;
30 conn.broadcast(("rooms:lobby", "user:entered"));
31 }
32
33 _ => {}
34 }
35 }
36
37 async fn incoming_message(&self, conn: ChannelConn<'_>, event: ChannelEvent) {
38 match (event.topic(), event.event()) {
39 ("rooms:lobby", "new:msg") => conn.broadcast(event),
40 _ => {}
41 }
42 }
43}
44
45// fn main() {
46// trillium_smol::run(channel(ChatChannel));
47// }
48```
49
50*/
51#[allow(unused_variables)]
52#[trillium::async_trait]
53pub trait ChannelHandler: Sized + Send + Sync + 'static {
54 /**
55 `connect` is called once when each websocket client is connected. The default implementation does nothing.
56 */
57 async fn connect(&self, conn: ChannelConn<'_>) {}
58
59 /**
60 `join_channel` is called when a websocket client sends a
61 `phx_join` event. There is no default implementation to ensure
62 that you implement the appropriate access control logic for your
63 application. If you want clients to be able to connect to any
64 channel they request, use this definition:
65
66 ```
67 # use trillium_channels::{ChannelEvent, ChannelConn, ChannelHandler};
68 # struct MyChannel; #[trillium::async_trait] impl ChannelHandler for MyChannel {
69 async fn join_channel(&self, conn: ChannelConn<'_>, event: ChannelEvent) {
70 conn.allow_join(&event, &()).await;
71 }
72 # }
73 ```
74 */
75 async fn join_channel(&self, conn: ChannelConn<'_>, event: ChannelEvent);
76
77 /**
78 `leave_channel` is called when a websocket client sends a
79 `phx_leave` event. The default implementation is to allow the user
80 to leave that channel.
81 */
82 async fn leave_channel(&self, conn: ChannelConn<'_>, event: ChannelEvent) {
83 conn.allow_leave(&event, &()).await
84 }
85
86 /**
87 `incoming_message` is called once for each [`ChannelEvent`] sent
88 from a client. The default implementation does nothing.
89 */
90 async fn incoming_message(&self, conn: ChannelConn<'_>, event: ChannelEvent) {}
91
92 /**
93 `disconnect` is called when the websocket client ceases to be
94 connected, either gracefully or abruptly.
95 */
96 async fn disconnect(&self, conn: ChannelConn<'_>) {}
97}