MessageHandler

Trait MessageHandler 

Source
pub trait MessageHandler:
    Send
    + Sync
    + 'static {
    type ClientMessage: DeserializeOwned + Send + Debug;
    type ServerEvent: Serialize + Send + Sync + Debug;
    type AppState: Send + Sync + 'static;
    type UserId: Send + Sync + Clone + Debug + 'static;

    // Required method
    fn handle_broadcast_message<'life0, 'life1, 'async_trait>(
        &'life0 self,
        msg: Self::ClientMessage,
        context: &'life1 ConnectionContext<Self::AppState, Self::UserId>,
    ) -> Pin<Box<dyn Future<Output = Result<Option<Self::ServerEvent>, HandlerError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;

    // Provided methods
    fn on_connect<'life0, 'life1, 'async_trait>(
        &'life0 self,
        _context: &'life1 ConnectionContext<Self::AppState, Self::UserId>,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn handle_direct_message<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        _msg: &'life1 Self::ClientMessage,
        _context: &'life2 ConnectionContext<Self::AppState, Self::UserId>,
    ) -> Pin<Box<dyn Future<Output = Result<Option<Value>, HandlerError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait { ... }
    fn on_disconnect<'life0, 'life1, 'async_trait>(
        &'life0 self,
        _context: &'life1 ConnectionContext<Self::AppState, Self::UserId>,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
}
Expand description

The central trait that a user’s application must implement.

This trait decouples the generic WebsocketService from any specific business logic, allowing users to “plug in” their own message handling, event types, and application state.

Required Associated Types§

Source

type ClientMessage: DeserializeOwned + Send + Debug

The type for messages coming from the client (e.g., a WsClientMessage enum). Must be deserializable from a string (e.g., JSON).

Source

type ServerEvent: Serialize + Send + Sync + Debug

The type for events broadcast via Redis (e.g., a ServerEvent enum). Must be serializable to a string (e.g., JSON).

Source

type AppState: Send + Sync + 'static

The shared application state (e.g., a struct holding database connection pools).

Source

type UserId: Send + Sync + Clone + Debug + 'static

The type for the user identifier. This can be i64, Uuid, String, etc.

Required Methods§

Source

fn handle_broadcast_message<'life0, 'life1, 'async_trait>( &'life0 self, msg: Self::ClientMessage, context: &'life1 ConnectionContext<Self::AppState, Self::UserId>, ) -> Pin<Box<dyn Future<Output = Result<Option<Self::ServerEvent>, HandlerError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Handles a message that may result in an event being broadcast to all subscribers.

This is ideal for state-changing operations like “create item” or “delete item”. If Ok(Some(event)) is returned, the event is serialized and published to

If Ok(None) is returned, the message was handled successfully, but no event needs to be broadcast.

Provided Methods§

Source

fn on_connect<'life0, 'life1, 'async_trait>( &'life0 self, _context: &'life1 ConnectionContext<Self::AppState, Self::UserId>, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

A lifecycle hook called immediately after a client successfully connects and is subscribed.

The default implementation does nothing.

Source

fn handle_direct_message<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, _msg: &'life1 Self::ClientMessage, _context: &'life2 ConnectionContext<Self::AppState, Self::UserId>, ) -> Pin<Box<dyn Future<Output = Result<Option<Value>, HandlerError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Handles a message that expects a direct response to the sender, not a broadcast.

This is ideal for read-only operations like “list items” or “get item”. If Ok(Some(response)) is returned, the response is serialized and sent directly to the client who sent the message.

If Ok(None) is returned, the WebsocketService will proceed to call handle_broadcast_message with the same message. This allows for a single message to potentially trigger both a direct response and a broadcast.

The default implementation returns Ok(None), falling back to the broadcast handler.

Source

fn on_disconnect<'life0, 'life1, 'async_trait>( &'life0 self, _context: &'life1 ConnectionContext<Self::AppState, Self::UserId>, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

A lifecycle hook called after a client has disconnected.

Useful for logging or performing cleanup related to the user’s session. The default implementation does nothing.

Implementors§