#ockam #crypto #network #networking #websocket


WebSocket Transport for the Ockam Routing Protocol

61 breaking releases

new 0.69.0 Mar 28, 2023
0.67.0 Feb 24, 2023
0.62.0 Sep 21, 2022
0.53.0 Jul 18, 2022
0.10.0 Jul 29, 2021

#166 in Cryptography

Download history 3/week @ 2022-11-30 4/week @ 2022-12-07 60/week @ 2022-12-14 5/week @ 2022-12-21 4/week @ 2022-12-28 4/week @ 2023-01-04 22/week @ 2023-01-11 26/week @ 2023-01-18 16/week @ 2023-01-25 146/week @ 2023-02-01 434/week @ 2023-02-08 296/week @ 2023-02-15 20/week @ 2023-02-22 24/week @ 2023-03-01 4/week @ 2023-03-08 1/week @ 2023-03-15

109 downloads per month




crate docs license discuss

Ockam is a library for building devices that communicate securely, privately and trustfully with cloud services and other devices.

This crate provides a WebSocket Transport for Ockam's Routing Protocol.

The Routing Protocol decouples Ockam's suite of cryptographic protocols, like secure channels, key lifecycle, credential exchange, enrollment etc. from the underlying transport protocols. This allows applications to establish end-to-end trust between entities.

WebSocket is one possible transport for Routing Protocol messages, over time there will be more transport implementations.


Add this to your Cargo.toml:

ockam_transport_websocket = "0.69.0"

This crate requires the rust standard library "std".

We need to define the behavior of the worker that will be processing incoming messages.

struct MyWorker;

impl Worker for MyWorker {
    type Context = Context;
    type Message = String;

    async fn handle_message(&mut self, ctx: &mut Context, msg: Routed<String>) -> Result<()> {
        // ...

Now we can write the main function that will run the previous worker. In this case, our worker will be listening for new connections on port 8000 until the process is manually killed.

use ockam_transport_websocket::WebSocketTransport;
use ockam::{Context, Result};

async fn main(mut ctx: Context) -> Result<()> {
    let ws = WebSocketTransport::create(&ctx).await?;
    ws.listen("localhost:8000").await?; // Listen on port 8000

    // Start a worker, of type MyWorker, at address "my_worker"
    ctx.start_worker("my_worker", MyWorker).await?;

    // Run worker indefinitely in the background

Finally, we can write another node that connects to the node that is hosting the MyWorker worker, and we are ready to send and receive messages between them.

use ockam_transport_websocket::{WebSocketTransport, WS};
use ockam::{Context, Result};

async fn main(mut ctx: Context) -> Result<()> {
    // Initialize the WS Transport.
    let _ws = WebSocketTransport::create(&ctx).await?;

    // Define the route to the server's worker.
    let r = route![(WS, "localhost:8000"), "my_worker"];

    // Now you can send messages to the worker.
    ctx.send(r, "Hello Ockam!".to_string()).await?;

    // Or receive messages from the server.
    let reply = ctx.receive::<String>().await?;

    // Stop all workers, stop the node, cleanup and return.


This code is licensed under the terms of the Apache License 2.0.


~171K SLoC