26 releases (14 breaking)

new 0.15.1 Nov 2, 2024
0.14.1 Aug 14, 2024
0.14.0 Jun 26, 2024
0.12.0 Mar 20, 2024
0.3.0 Jun 17, 2023

#221 in WebSocket

Download history 2594/week @ 2024-07-18 3087/week @ 2024-07-25 2866/week @ 2024-08-01 2975/week @ 2024-08-08 2887/week @ 2024-08-15 2959/week @ 2024-08-22 2219/week @ 2024-08-29 1785/week @ 2024-09-05 2661/week @ 2024-09-12 2851/week @ 2024-09-19 3176/week @ 2024-09-26 3310/week @ 2024-10-03 3969/week @ 2024-10-10 3662/week @ 2024-10-17 3433/week @ 2024-10-24 3818/week @ 2024-10-31

15,738 downloads per month
Used in 9 crates (2 directly)

MIT license

165KB
3.5K SLoC

Engineioxide does the heavy lifting for socketioxide, a socket.io server implementation in Rust which integrates with the tower stack.

You can still use engineioxide as a standalone crate to talk with an engine.io client.

Supported Protocols

You can enable support for other engine.io protocol implementations through feature flags. The latest protocol version (v4) is enabled by default.

To add support for the v3 protocol version, adjust your dependency configuration accordingly:

[dependencies]
# Enables the `v3` protocol (`v4` is always enabled, as it's the default).
engineioxide = { version = "0.3.0", features = ["v3"] }

Feature flags :

  • v3: Enable the engine.io v3 protocol
  • tracing: Enable tracing logs with the tracing crate

Basic example with axum :

use bytes::Bytes;
use engineioxide::layer::EngineIoLayer;
use engineioxide::handler::EngineIoHandler;
use engineioxide::{Socket, DisconnectReason, Str};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use axum::routing::get;
// Global state, with axum it must be clonable
#[derive(Debug, Default, Clone)]
struct MyHandler {
    user_cnt: Arc<AtomicUsize>,
}

// Socket state
#[derive(Debug, Default)]
struct SocketState {
    id: Mutex<String>,
}

impl EngineIoHandler for MyHandler {
    type Data = SocketState;

    fn on_connect(self: Arc<Self>, socket: Arc<Socket<SocketState>>) {
        let cnt = self.user_cnt.fetch_add(1, Ordering::Relaxed) + 1;
        socket.emit(cnt.to_string()).ok();
    }
    fn on_disconnect(&self,
        socket: Arc<Socket<SocketState>>,
        reason: DisconnectReason
    ) {
        let cnt = self.user_cnt.fetch_sub(1, Ordering::Relaxed) - 1;
        socket.emit(cnt.to_string()).ok();
    }
    fn on_message(&self, msg: Str, socket: Arc<Socket<SocketState>>) {
        *socket.data.id.lock().unwrap() = msg.into(); // bind a provided user id to a socket
    }
    fn on_binary(&self, data: Bytes, socket: Arc<Socket<SocketState>>) { }
}

// Create a new engineio layer
let layer = EngineIoLayer::new(Arc::new(MyHandler::default()));

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

// Spawn the axum server

Dependencies

~6–15MB
~193K SLoC