⚡Socketioxide - SocketIO server powered by Rust! 🦀
Tags: rust, socketio, opensource, github
Published: 2023-10-10 18:37:34

> ### *Rust is hard, so is writing networking code in it. But who doesn’t want its blazing fast speed? Socketioxide fills the gap. It’s socket.io, but with speed and safety, that helps you achieve unimaginable while keeping it dead simple.*

[Socket.io](https://socket.io) is a massively popular websocket/polling framework with about 60k stars on github. Socket.io server is officially written in Node.js, which was ported to Python, Go (upto v1.4), Java and Swift by the community. Now, there is a Rust port as well, **Socketioxide**:

{% embed https://github.com/Totodore/socketioxide %}

## 🎉 Latest Release (`v0.8.0`)
`v0.8.0` was released on Dec 12. See the full release note here: https://github.com/Totodore/socketioxide/releases/tag/v0.8.0

## 💎 Socketioxide Features

1. Integrates with:
 - [Axum](https://docs.rs/axum/latest/axum/)
 - [Warp](https://docs.rs/warp/latest/warp/)
 - [Hyper](https://docs.rs/hyper/latest/hyper/)
 - [Salvo](https://docs.rs/salvo/latest/salvo/)
2. Out of the box support for any other middleware based on tower :
 - [CORS](https://docs.rs/tower-http/latest/tower_http/cors)
 - [Compression](https://docs.rs/tower-http/latest/tower_http/compression)
 - [Authorization](https://docs.rs/tower-http/latest/tower_http/auth)
11. Namespaces
12. Rooms
13. Ack and emit with ack
14. Binary packets
15. Polling & Websocket transports
16. Extensions to add custom data to sockets
17. Memory efficient http payload parsing with streams
18. Api that mimics the [socket.io](https://socket.io/docs/v4/server-api/) JavaScript api as much as possible
19. Well tested with the official [end to end test-suite](https://github.com/totodore/socketioxide/actions)
20. Socket.io versions supported :
 - [_protocol v5_](https://socket.io/docs/v4/) : based on engine.io v4 under the feature flag v5 (default), (socket.io js from v3.0.0..latest)
 - [_protocol v4_](https://github.com/socketio/socket.io-protocol/tree/v4) : based on engine.io v3, feature flag v4, (socket.io js from v1.0.3..latest)


## 🪄 Power of Rust + Simplicity of Socket IO

The thing about socket.io is that it’s so simple, that someone who just learned JavaScript can get started with it. Write an html page, bring the client and hook some event listeners.

On the other hand, Rust is a language with powerful type system and runtime speed. Programs written in Rust are more memory safe than C, C++ etc. That makes Rust a great choice for both mission critical and general purpose software development.

Now that you can combine the simplicity and power together, Socketioxide emerges as a great choice for realtime apps.

## 🧑‍💻 How to get started with it?
This is an example of socket.io echo server with [Socketioxide](https://github.com/Totodore/socketioxide) and [Axum](https://github.com/tokio-rs/axum)
```rs
use axum::routing::get;
use axum::Server;
use serde_json::Value;
use socketioxide::SocketIo;
use tracing::info;
use tracing_subscriber::FmtSubscriber;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    tracing::subscriber::set_global_default(FmtSubscriber::default())?;

    let (layer, io) = SocketIo::new_layer();
    io.ns("/", |socket, auth: Value| async move {
        info!("Socket.IO connected: {:?} {:?}", socket.ns(), socket.sid);
        socket.emit("auth", auth).ok();

        socket.on("message", |socket, data: Value, bin, _| async move {
            info!("Received event: {:?} {:?}", data, bin);
            socket.bin(bin).emit("message-back", data).ok();
        });

        socket.on("message-with-ack", |_, data: Value, bin, ack| async move {
            info!("Received event: {:?} {:?}", data, bin);
            ack.bin(bin).send(data).ok();
        });

        socket.on_disconnect(|socket, reason| async move {
            info!("Socket.IO disconnected: {} {}", socket.sid, reason);
        });
    });

    io.ns("/custom", |socket, auth: Value| async move {
        info!("Socket.IO connected on: {:?} {:?}", socket.ns(), socket.sid);
        socket.emit("auth", auth).ok();
    });

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

    info!("Starting server");

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

    Ok(())
}
```

If you want to build a chat app, here is an example to get you started: https://github.com/Totodore/socketioxide/tree/main/examples/src/chat

If you are planning to use a frontend framework like React, Angular or Svelte, consult the official docs: https://socket.io/docs/v3/client-initialization/

Try it out and star⭐ on Github!