Skip to content

How to access SocketIO state in axum handler? #335

Answered by Totodore
fra-luc asked this question in Q&A
Discussion options

You must be logged in to vote

Hi ! You have to wrap your state in an Arc<T> so that it is cheaply cloneable and that you have a reference in the state anywhere (it is required by axum and will soon be required by socketioxide also).
Then you can either create your own axum State struct and put your socket.io Arc<SocketIoState> in it or you can directly use the state you set for socket.io as an axum state:

Here is an example with a custom State structs that wraps store::Messages and SocketIo.

use axum::routing::post;
use socketioxide::{
    extract::{Data, SocketRef, State},
    SocketIo,
};
use tokio::net::TcpListener;
use tower::ServiceBuilder;
use tower_http::cors::CorsLayer;
use tracing::info;
use tracing_subscriber::

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by Totodore
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants