Skip to main content

WebSocketMiddleware

Trait WebSocketMiddleware 

Source
pub trait WebSocketMiddleware<S: AppState>:
    Send
    + Sync
    + 'static {
    // Provided methods
    fn on_send<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        message: &'life1 Message,
        context: &'life2 MiddlewareContext<S>,
    ) -> Pin<Box<dyn Future<Output = CoreResult<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait { ... }
    fn on_receive<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        message: &'life1 Message,
        context: &'life2 MiddlewareContext<S>,
    ) -> Pin<Box<dyn Future<Output = CoreResult<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait { ... }
    fn on_connect<'life0, 'life1, 'async_trait>(
        &'life0 self,
        context: &'life1 MiddlewareContext<S>,
    ) -> Pin<Box<dyn Future<Output = CoreResult<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn on_disconnect<'life0, 'life1, 'async_trait>(
        &'life0 self,
        context: &'life1 MiddlewareContext<S>,
    ) -> Pin<Box<dyn Future<Output = CoreResult<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn on_connection_attempt<'life0, 'life1, 'async_trait>(
        &'life0 self,
        _context: &'life1 MiddlewareContext<S>,
    ) -> Pin<Box<dyn Future<Output = CoreResult<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn on_connection_failure<'life0, 'life1, 'async_trait>(
        &'life0 self,
        _context: &'life1 MiddlewareContext<S>,
        _reason: Option<String>,
    ) -> Pin<Box<dyn Future<Output = CoreResult<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
}
Expand description

Trait for implementing WebSocket middleware.

Middleware can observe and react to WebSocket messages being sent and received. This trait provides hooks for both outgoing and incoming messages.

§Type Parameters

  • S: The application state type that implements AppState

§Methods

  • [on_send]: Called before a message is sent to the WebSocket
  • [on_receive]: Called after a message is received from the WebSocket
  • [on_connect]: Called when a WebSocket connection is established
  • [on_disconnect]: Called when a WebSocket connection is lost

§Error Handling

Middleware should be designed to be resilient. If middleware returns an error, it will be logged but will not prevent the message from being processed or other middleware from running.

Provided Methods§

Source

fn on_send<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, message: &'life1 Message, context: &'life2 MiddlewareContext<S>, ) -> Pin<Box<dyn Future<Output = CoreResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Called before a message is sent to the WebSocket.

§Arguments
  • message: The message that will be sent
  • context: Context information including state and sender
§Returns
  • Ok(()) if the middleware processed successfully
  • Err(_) if an error occurred (will be logged but not block processing)
Source

fn on_receive<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, message: &'life1 Message, context: &'life2 MiddlewareContext<S>, ) -> Pin<Box<dyn Future<Output = CoreResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Called after a message is received from the WebSocket.

§Arguments
  • message: The message that was received
  • context: Context information including state and sender
§Returns
  • Ok(()) if the middleware processed successfully
  • Err(_) if an error occurred (will be logged but not block processing)
Source

fn on_connect<'life0, 'life1, 'async_trait>( &'life0 self, context: &'life1 MiddlewareContext<S>, ) -> Pin<Box<dyn Future<Output = CoreResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Called when a WebSocket connection is established.

§Arguments
  • context: Context information including state and sender
§Returns
  • Ok(()) if the middleware processed successfully
  • Err(_) if an error occurred (will be logged but not block processing)
Source

fn on_disconnect<'life0, 'life1, 'async_trait>( &'life0 self, context: &'life1 MiddlewareContext<S>, ) -> Pin<Box<dyn Future<Output = CoreResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Called when a WebSocket connection is lost.

§Arguments
  • context: Context information including state and sender
§Returns
  • Ok(()) if the middleware processed successfully
  • Err(_) if an error occurred (will be logged but not block processing)
Source

fn on_connection_attempt<'life0, 'life1, 'async_trait>( &'life0 self, _context: &'life1 MiddlewareContext<S>, ) -> Pin<Box<dyn Future<Output = CoreResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Called when a connection attempt is made (before actual connection)

Source

fn on_connection_failure<'life0, 'life1, 'async_trait>( &'life0 self, _context: &'life1 MiddlewareContext<S>, _reason: Option<String>, ) -> Pin<Box<dyn Future<Output = CoreResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Called when a connection attempt fails

Implementors§