Skip to main content

websocket_handler

Function websocket_handler 

Source
pub async fn websocket_handler<H: WebSocketHandler + 'static>(
    ws: WebSocketUpgrade,
    __arg1: State<WebSocketState<H>>,
) -> impl IntoResponse
Expand description

WebSocket upgrade handler

This is the main entry point for WebSocket connections. Use this as an Axum route handler by passing it to an Axum router’s .route() method with get().

§Arguments

  • ws - WebSocket upgrade from Axum
  • State(state) - Application state containing the handler and optional schemas

§Returns

An Axum response that upgrades the connection to WebSocket

§Example

use axum::{Router, routing::get, extract::State};

let state = WebSocketState::new(MyHandler);
let router = Router::new()
    .route("/ws", get(websocket_handler::<MyHandler>))
    .with_state(state);