#websockets #websocket #no-std

no-std embedded-websocket

A lightweight websocket library for embedded systems (no_std)

11 releases (7 breaking)

0.9.3 Jun 26, 2023
0.8.0 Apr 26, 2021
0.6.0 Apr 16, 2021
0.5.0 Feb 2, 2021
0.1.0 Jun 18, 2019

#5 in #sec-websocket

Download history 45/week @ 2023-11-02 110/week @ 2023-11-09 46/week @ 2023-11-16 85/week @ 2023-11-23 94/week @ 2023-11-30 68/week @ 2023-12-07 170/week @ 2023-12-14 50/week @ 2023-12-21 43/week @ 2023-12-28 51/week @ 2024-01-04 27/week @ 2024-01-11 50/week @ 2024-01-18 57/week @ 2024-01-25 52/week @ 2024-02-01 115/week @ 2024-02-08 203/week @ 2024-02-15

432 downloads per month
Used in pipebuf_websocket




A lightweight rust websocket library for embedded systems no_std

This library facilitates the encoding and decoding of websocket messages and can be used for both clients and servers. The library is intended to be used in constrained memory environments like embedded microcontrollers which cannot reference the rust standard library. The library will work with arbitrarily small buffers regardless of websocket frame size as long as the websocket header can be read (2 - 14 bytes depending)

no_std support

You can use this library without linking the rust standard library. In your Cargo.toml file make sure you set the default features to false. For example:

embedded-websocket = { version = "x.x.x", default-features = false }

The optional (but recommended) framer module allows you to ergonomically work with full websocket frames without having to deal with the complexities of fragmented data. If you use it you will have to implement the Read and Write traits in that module because they are not available in no_std.

NOTE: If you get an error message like the following it means that you have not used default-features = false when declaring the dependency in your Cargo.toml file:

error[E0463]: can't find crate for `std`
  = note: the `thumbv7m-none-eabi` target may not be installed

Running the examples

The example below runs a websocket server that accepts client connections in a loop and returns back whatever text client sends. The client connects to the server, sends one "Hello, World!" message, waits for a response from the server then disconnects and terminates. Proper open and close handshakes are demonstrated.

To run the demo web server:

cargo run --example server

To run the demo websocket client:

cargo run --example client

or use this url in your browser

Working example project

See https://github.com/ninjasource/led-display-websocket-demo for a complete end-to-end example of this library working with and stm32 m3 bluepill MCU and an embedded ethernet card.

Example websocket client usage:

The following example also found here initiates a opening handshake, checks the handshake response, sends a short message, initiates a close handshake, checks the close handshake response and quits.

// open a TCP stream to localhost port 1337
let address = "";
println!("Connecting to: {}", address);
let mut stream = TcpStream::connect(address).map_err(FramerError::Io)?;

let mut read_buf = [0; 4000];
let mut read_cursor = 0;
let mut write_buf = [0; 4000];
let mut frame_buf = [0; 4000];
let mut websocket = WebSocketClient::new_client(rand::thread_rng());

// initiate a websocket opening handshake
let websocket_options = WebSocketOptions {
    path: "/chat",
    host: "localhost",
    origin: "http://localhost:1337",
    sub_protocols: None,
    additional_headers: None,

let mut framer = Framer::new(
    &mut read_buf,
    &mut read_cursor,
    &mut write_buf,
    &mut websocket,
framer.connect(&mut stream, &websocket_options)?;

let message = "Hello, World!";
    &mut stream,

while let Some(s) = framer.read_text(&mut stream, &mut frame_buf)? {
    println!("Received: {}", s);

    // close the websocket after receiving the first reply
    framer.close(&mut stream, WebSocketCloseStatusCode::NormalClosure, None)?;
    println!("Sent close handshake");

println!("Connection closed");

Example websocket server usage

The server example is a little too verbose to include in this readme, See server example

Async support

Async support is experimental and subject to significant API change.


Licensed under either MIT or Apache-2.0 at your option


~44K SLoC