#websocket #webassembly #stateroom


A macro for building a Stateroom service as a WebAssembly module

8 releases

0.2.8 Feb 20, 2023
0.2.7 Feb 20, 2023
0.2.6 Sep 20, 2022
0.2.4 Aug 22, 2022
0.2.0 Jan 20, 2022

#362 in WebAssembly

Download history 9/week @ 2022-11-20 6/week @ 2022-11-27 1/week @ 2022-12-04 8/week @ 2022-12-11 2/week @ 2022-12-25 4/week @ 2023-01-08 10/week @ 2023-01-15 10/week @ 2023-01-22 24/week @ 2023-01-29 30/week @ 2023-02-05 38/week @ 2023-02-12 49/week @ 2023-02-19 8/week @ 2023-02-26

95 downloads per month


194 lines


stateroom-wasm is a companion crate to stateroom that helps you package and export services as WebAssembly modules.

#[stateroom_wasm] macro

WebAssembly modules must import and export certain named functions in order for the Stateroom server to understand them. stateroom_wasm provides the #[stateroom_wasm] module, which should be applied to an item (struct, enum, or type alias) that implements SimpleStateroomService.

A Stateroom-compatible WebAssembly module must contain exactly one service. If you need to generate multiple services for your application, currently the best approach is to make a crate for each service.

It's possible to generate bindings for a service that belongs to another module using a type alias:

use stateroom_wasm::stateroom_wasm;
use some_module::SomeService;

type Service = SomeService;

Execution model

Upon initialization, the generated model creates an instance of your SimpleStateroomService by calling its new(room_id, context) constructor. The context object that is passed in the constructor and subsequent function calls is a global static object that binds to functions imported from the host environment (like send_message).


If you are using the Stateroom command-line interface, stateroom dev will build the current crate using the wasm32-wasi target, and then load and serve the generated WebAssembly module.

If you would like to build it manually, make sure you have the wasm32-wasi target installed and pass it as a target to cargo build:

$ rustup target add wasm32-wasi
$ cargo build --release --target=wasm32-wasi


This crate has a counterpart, stateroom-wasm-host, which can take a module generated with this crate and expose it through a StateroomService interface.


~26K SLoC