20 releases (11 breaking)

0.12.0 Mar 20, 2024
0.10.2 Jan 22, 2024
0.9.1 Dec 21, 2023
0.7.2 Nov 22, 2023
0.3.0 Jun 17, 2023

#115 in WebSocket

Download history 699/week @ 2023-12-23 992/week @ 2023-12-30 1475/week @ 2024-01-06 1205/week @ 2024-01-13 1532/week @ 2024-01-20 1504/week @ 2024-01-27 1721/week @ 2024-02-03 1378/week @ 2024-02-10 1243/week @ 2024-02-17 1335/week @ 2024-02-24 1756/week @ 2024-03-02 1842/week @ 2024-03-09 1694/week @ 2024-03-16 1426/week @ 2024-03-23 1546/week @ 2024-03-30 1467/week @ 2024-04-06

6,370 downloads per month
Used in 5 crates (via socketioxide)

MIT license

155KB
3K 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 engineioxide::layer::EngineIoLayer;
use engineioxide::handler::EngineIoHandler;
use engineioxide::{Socket, DisconnectReason};
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, 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: String, socket: Arc<Socket<SocketState>>) { 
        *socket.data.id.lock().unwrap() = msg; // bind a provided user id to a socket
    }
    fn on_binary(&self, data: Vec<u8>, socket: Arc<Socket<SocketState>>) { }
}

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

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

// Spawn the axum server

Dependencies

~9–21MB
~262K SLoC