#ockam #crypto #network #networking #websocket

no-std ockam_transport_websocket

WebSocket Transport for the Ockam Routing Protocol

89 breaking releases

0.97.0 Jan 9, 2024
0.95.0 Dec 29, 2023
0.87.0 Nov 8, 2023
0.75.0 Jun 27, 2023
0.10.0 Jul 29, 2021

#105 in Cryptography

Download history 111/week @ 2023-11-01 43/week @ 2023-11-08 13/week @ 2023-11-15 94/week @ 2023-11-22 323/week @ 2023-11-29 193/week @ 2023-12-06 172/week @ 2023-12-13 116/week @ 2023-12-20 215/week @ 2023-12-27 68/week @ 2024-01-03 16/week @ 2024-01-10 127/week @ 2024-01-17 91/week @ 2024-01-24 95/week @ 2024-01-31 1/week @ 2024-02-07 1667/week @ 2024-02-14

1,886 downloads per month

Apache-2.0

490KB
10K SLoC

ockam_transport_websocket

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.

This crate requires the rust standard library "std".

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

use ockam_core::{Worker, Result, Routed, async_trait};
use ockam_node::Context;

struct MyWorker;

#[async_trait]
impl Worker for MyWorker {
    type Context = Context;
    type Message = String;

    async fn handle_message(&mut self, _ctx: &mut Context, _msg: Routed<String>) -> Result<()> {
        // ...
        Ok(())
    }
}

// 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_node::NodeBuilder;
use ockam_macros::node;

#[ockam_macros::node(crate = "ockam_node")]
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
    Ok(())
}

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_core::{route, Result};
use ockam_node::Context;
use ockam_macros::node;

#[ockam_macros::node(crate = "ockam_node")]
async fn main(mut ctx: Context) -> Result<()> {
    use ockam_node::MessageReceiveOptions;
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.
    ctx.stop().await
}

Usage

Add this to your Cargo.toml:

[dependencies]
ockam_transport_websocket = "0.97.0"

License

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

Dependencies

~4–15MB
~167K SLoC