Socketioxide: SocketIO & EngineIO Server in Rust

SocketIO CI

Socket.IO server implementation as a tower layer in Rust

It integrates with any framework based on tower/hyper, such as: * axum * warp * hyper

It takes full advantage of the tower and tower-http ecosystem of middleware, services, and utilities.

⚠️ This crate is under active development and the API is not yet stable.

Features :

Planned features :

Examples :

[derive(Debug, Serialize, Deserialize)]

struct MyData { pub name: String, pub age: u8, }

[tokio::main]

async fn main() -> Result<(), Box> {

println!("Starting server");

let ns = Namespace::builder()
    .add("/", |socket| async move {
        println!("Socket connected on / namespace with id: {}", socket.sid);

        // Add a callback triggered when the socket receive an 'abc' event
        // The json data will be deserialized to MyData
        socket.on("abc", |socket, data: MyData, bin, _| async move {
            println!("Received abc event: {:?} {:?}", data, bin);
            socket.bin(bin).emit("abc", data).ok();
        });

        // Add a callback triggered when the socket receive an 'acb' event
        // Ackknowledge the message with the ack callback
        socket.on("acb", |_, data: Value, bin, ack| async move {
            println!("Received acb event: {:?} {:?}", data, bin);
            ack.bin(bin).send(data).ok();
        });
    })
    .add("/custom", |socket| async move {
        println!("Socket connected on /custom namespace with id: {}", socket.sid);
    })
    .build();

let app = axum::Router::new()
    .route("/", get(|| async { "Hello, World!" }))
    .layer(SocketIoLayer::new(ns));

Server::bind(&"0.0.0.0:3000".parse().unwrap())
    .serve(app.into_make_service())
    .await?;

Ok(())

} ```