SockJS server for rust language

Overview

SockJS server Build Status codecov crates.io

SockJS server for Actix framework.


Actix SockJS is licensed under the Apache-2.0 license.

Usage

To use sockjs, add this to your Cargo.toml:

[dependencies]
sockjs = "0.2"

Supported transports

Simple chat example

extern crate actix;
extern crate actix_web;
extern crate sockjs;

use actix_web::*;
use actix::prelude::*;
use sockjs::{Message, Session, CloseReason, SockJSManager, SockJSContext};

struct Chat;

/// `SockJSContext` context is required for sockjs session
impl Actor for Chat {
    type Context = SockJSContext<Self>;
}

/// Session has to implement `Default` trait
impl Default for Chat {
    fn default() -> Chat { Chat }
}

/// Sockjs session trait
impl Session for Chat {
    fn opened(&mut self, ctx: &mut SockJSContext<Self>) {
        ctx.broadcast("Someone joined.")
    }
    fn closed(&mut self, ctx: &mut SockJSContext<Self>, _: CloseReason) {
        ctx.broadcast("Someone left.")
    }
}

/// Session has to be able to handle `sockjs::Message` messages
impl Handler<Message> for Chat {
    type Result = ();

    fn handle(&mut self, msg: Message, ctx: &mut SockJSContext<Self>)
    {
        // broadcast message to all sessions
        ctx.broadcast(msg);
    }
}


fn main() {
    let sys = actix::System::new("sockjs-chat");

    // SockJS sessions manager
    let sm: Addr<Syn, _> = SockJSManager::<Chat>::start_default();

    HttpServer::new(move || {
        let manager = sm.clone();
        Application::new()
            // register SockJS application
            .handler(
                "/sockjs/", sockjs::SockJS::new(manager.clone()))})
        .bind("127.0.0.1:8080").unwrap()
        .start();

    // let _ = sys.run();
}

Full chat example

You might also like...
Composable WebSockets made easy, for Rust 🦀

ezsockets Have you ever struggle with creating a WebSocket server or a client in Rust? This crate is for you. High level abstraction of WebSocket, han

Rust API connector for Bybit's WebSockets APIs.

rust-bybit English | 简体中文 Unofficial Rust API connector for Bybit's WebSockets APIs. Disclaimer This is an unofficial Rust API connector for Bybit's A

A MITM Proxy Written in Rust 🦀! Toolkit for HTTP/1, HTTP/2, and WebSockets with SSL/TLS Capabilities. Learning Project.
A MITM Proxy Written in Rust 🦀! Toolkit for HTTP/1, HTTP/2, and WebSockets with SSL/TLS Capabilities. Learning Project.

Man In The Middle Proxy Description Rust-based Man in the Middle proxy, an early-stage project aimed at providing visibility into network traffic. Cur

Easy c̵̰͠r̵̛̠ö̴̪s̶̩̒s̵̭̀-t̶̲͝h̶̯̚r̵̺͐e̷̖̽ḁ̴̍d̶̖̔ ȓ̵͙ė̶͎ḟ̴͙e̸̖͛r̶̖͗ë̶̱́ṉ̵̒ĉ̷̥e̷͚̍ s̷̹͌h̷̲̉a̵̭͋r̷̫̊ḭ̵̊n̷̬͂g̵̦̃ f̶̻̊ơ̵̜ṟ̸̈́ R̵̞̋ù̵̺s̷̖̅ţ̸͗!̸̼͋

Rust S̵̓i̸̓n̵̉ I̴n̴f̶e̸r̵n̷a̴l mutability! Howdy, friendly Rust developer! Ever had a value get m̵̯̅ð̶͊v̴̮̾ê̴̼͘d away right under your nose just when

AIDL Language Server Protocol (LSP) server

AIDL Language Server Protocol (LSP) server Experimental AIDL LSP server based on rust-aidl-parser. Features: diagnostics workspace symbols (Ctrl+T in

A LSP (Language Server Protocol) server for OpenSCAD.

openscad-LSP A LSP (Language Server Protocol) server for OpenSCAD. inspired by dzhu/openscad-language-server Tested with VSCode on Mac and Windows. Te

impl LSP (Language Server Protocol) Server for librime

rime-ls 为 rime 输入法核心库 librime (的部分功能) 实现 LSP 协议, 从而通过编辑器的代码补全功能输入汉字. 项目还处在早期阶段, 各方面都非常不成熟. 目标是提供 rime + LSP 的通用解决方案, 在不同编辑器内实现与其他 rime 前端类似的输入体验. Feat

A language server implementation for the WGSL shading language

wgsl-analyzer wgsl-analyzer is a language server plugin for the WGSL Shading language. It comes with a VS Code plugin located in ./editors/code, but d

DNS Server written in Rust for fun, see https://dev.to/xfbs/writing-a-dns-server-in-rust-1gpn

DNS Fun Ever wondered how you can write a DNS server in Rust? No? Well, too bad, I'm telling you anyways. But don't worry, this is going to be a fun o

Dav-server-rs - Rust WebDAV server library. A fork of the webdav-handler crate.

dav-server-rs A fork of the webdav-handler-rs project. Generic async HTTP/Webdav handler Webdav (RFC4918) is defined as HTTP (GET/HEAD/PUT/DELETE) plu

axum-server is a hyper server implementation designed to be used with axum framework.

axum-server axum-server is a hyper server implementation designed to be used with axum framework. Features Conveniently bind to any number of addresse

Jex Compiler Server - Server that runs Jex code

Server that compiles and runs Jex code.

Solana Game Server is a decentralized game server running on Solana, designed for game developers

Solana Game Server* is the first decentralized Game Server (aka web3 game server) designed for game devs. (Think web3 SDK for game developers as a ser

Live Server - Launch a local network server with live reload feature for static pages

Live Server - Launch a local network server with live reload feature for static pages

Static Web Server - a very small and fast production-ready web server suitable to serve static web files or assets
Static Web Server - a very small and fast production-ready web server suitable to serve static web files or assets

Static Web Server (or SWS abbreviated) is a very small and fast production-ready web server suitable to serve static web files or assets.

A simple web server(and library) to display server stats over HTTP and Websockets/SSE or stream it to other systems.

x-server-stats A simple web server(and library) to display server stats over HTTP and Websockets/SSE or stream it to other systems. x-server(in x-serv

Bruteforce connecting to a specific Sea of Thieves server. Useful if you want to be in the same server as your friends.

SoT Server Finder Find which Sea of Thieves server you're connected to. Useful if you want to be in the same server as your friends. Setup Download so

QUIC proxy that allows to use QUIC to connect to an SSH server without needing to patch the client or the server.

quicssh-rs 😄 quicssh-rs is a QUIC proxy that allows to use QUIC to connect to an SSH server without needing to patch the client or the server. quicss

Leptos server signals synced through Server-Sent-Events (SSE)

Leptos Server Sent Events Server signals are leptos signals kept in sync with the server through server-sent-events (SSE). The signals are read-only o

Comments
  • Non-default Actor

    Non-default Actor

    On this line here: https://github.com/actix/sockjs/blob/master/examples/chat.rs#L54

    What is the correct way to create a non-default Actor/etc that isn't generated from Default::default()?

    opened by enzious 39
  • I would like to contribute

    I would like to contribute

    Hey there, I would like to contribute to this project, although I'm relatively new to rust but if anyone can point me to the right direction towards how to go about upgrading it to the latest actix-web would be a good start.
    I'm at your disposal 🤣 Thanks

    opened by mygnu 5
  • error: multiple packages link to native library `ring-asm`, but a native library can be linked only once

    error: multiple packages link to native library `ring-asm`, but a native library can be linked only once

    Can't build with latest version of actix-web. Here is the error:

    C:\Users\candronikos\Projects\project>cargo build
    error: multiple packages link to native library `ring-asm`, but a native library can be linked only once
    
    package `ring v0.13.2`
        ... which is depended on by `cookie v0.11.0`
        ... which is depended on by `actix-web v0.7.3`
        ... which is depended on by `project v0.1.0 (file:///C:/Users/candronikos/Projects/project)`
    links to native library `ring-asm`
    
    package `ring v0.12.1`
        ... which is depended on by `cookie v0.10.1`
        ... which is depended on by `actix-web v0.5.8`
        ... which is depended on by `sockjs v0.3.0`
        ... which is depended on by `project v0.1.0 (file:///C:/Users/candronikos/Projects/project)`
    also links to native library `ring-asm`
    
    opened by candronikos 3
  • `()` doesn't implement `std::fmt::Display`

    `()` doesn't implement `std::fmt::Display`

    rustc 1.26.0 (a77568041 2018-05-07)
    
    error[E0277]: `()` doesn't implement `std::fmt::Display`
      --> /Users/intellild/.cargo/registry/src/mirrors.ustc.edu.cn-61ef6e0cd06fb9b8/sockjs-0.3.0/src/transports/xhrsend.rs:89:49
       |
    89 |                                             Err(error::ErrorNotFound(())),
       |                                                 ^^^^^^^^^^^^^^^^^^^^ `()` cannot be formatted with the default formatter; try using `:?` instead if you are using a format string
       |
       = help: the trait `std::fmt::Display` is not implemented for `()`
       = note: required by `actix_web::error::ErrorNotFound`
    
    error[E0277]: `()` doesn't implement `std::fmt::Display`
       --> /Users/intellild/.cargo/registry/src/mirrors.ustc.edu.cn-61ef6e0cd06fb9b8/sockjs-0.3.0/src/transports/jsonp.rs:271:49
        |
    271 |                                             Err(error::ErrorNotFound(())),
        |                                                 ^^^^^^^^^^^^^^^^^^^^ `()` cannot be formatted with the default formatter; try using `:?` instead if you are using a format string
        |
        = help: the trait `std::fmt::Display` is not implemented for `()`
        = note: required by `actix_web::error::ErrorNotFound`
    
    error: aborting due to 2 previous errors
    
    For more information about this error, try `rustc --explain E0277`.
    error: Could not compile `sockjs`.
    
    opened by intellild 0
Releases(v0.3.0)
Owner
Actix
Actix - web and actor frameworks for Rust
Actix
"Last Mile" streaming server and client

TSLM - Terminal Stream Last Mile This is an asynchronous WebSocket server written in Rust using tokio-tungstenite. It allows a WebSocket client to per

null 4 Nov 9, 2023
A WebSocket (RFC6455) library written in Rust

Rust-WebSocket Note: Maintainership of this project is slugglish. You may want to use tungstenite or tokio-tungstenite instead. Rust-WebSocket is a We

Rust Websockets 1.3k Jan 6, 2023
Lightweight, event-driven WebSockets for Rust.

WS-RS Lightweight, event-driven WebSockets for Rust. /// A WebSocket echo server listen("127.0.0.1:3012", |out| { move |msg| { out.send(ms

Jason Housley 1.3k Jan 8, 2023
Lightweight stream-based WebSocket implementation for Rust.

Tungstenite Lightweight stream-based WebSocket implementation for Rust. use std::net::TcpListener; use std::thread::spawn; use tungstenite::server::ac

Snapview GmbH 1.3k Jan 2, 2023
A very-very simple url shortener for Rust

urlshortener-rs A very simple urlshortener for Rust. This library aims to implement as much URL shortener services as possible and to provide an inter

Victor Polevoy 39 Nov 20, 2022
Synchronized state machines for Rust over WebSockets.

Aper is a framework for real-time sharing of application state over WebSockets.

null 191 Dec 20, 2022
A WebSocket (RFC6455) library written in Rust

Rust-WebSocket Rust-WebSocket is a WebSocket (RFC6455) library written in Rust. Rust-WebSocket provides a framework for dealing with WebSocket connect

Jason N 19 Aug 22, 2022
An aria2 websocket jsonrpc in Rust.

aria2-ws An aria2 websocket jsonrpc in Rust. Built with tokio. Docs.rs aria2 RPC docs Features Almost all methods and structed responses Auto reconnec

null 8 Sep 7, 2022
Rust + wasm + websockets

This is a template repo for eframe, a framework for writing apps using egui.

Emil Ernerfeldt 12 Oct 3, 2022
A simple toy websocket client to connect to Bitstamp.net and print the live order book written in Rust.

A simple toy websocket client to connect to Bitstamp.net and print the live order book written in Rust.

Nate Houk 1 Feb 14, 2022