It integrates with any framework based on tower/hyper, such as: * axum: echo implementation * warp: echo implementation * hyper: echo implementation
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.
struct MyData { pub name: String, pub age: u8, }
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(&"127.0.0.1:3000".parse().unwrap())
.serve(app.into_make_service())
.await?;
Ok(())
} ```