mezzenger-webworker

Crates.iomezzenger-webworker
lib.rsmezzenger-webworker
version0.1.3
sourcesrc
created_at2022-11-11 14:44:22.977715
updated_at2023-09-19 12:52:43.315668
descriptionTransport for communicating with web workers.
homepagehttps://github.com/zduny/mezzenger/tree/master/mezzenger-webworker
repositoryhttps://github.com/zduny/mezzenger
max_upload_size
id712886
size28,014
Daniel Zduniak (zduny)

documentation

https://docs.rs/mezzenger-webworker/

README

mezzenger-webworker

Transport for communication with Web Workers.

https://crates.io/crates/mezzenger-webworker

ko-fi

usage

Add dependencies to Cargo.toml:

[dependencies]
# ...
serde = { version = "1", features = ["derive"] }
kodec = { version = "0.1.0", features = ["binary"] } # or json or different one from another crate...
mezzenger = "0.1.3"
mezzenger-webworker = "0.1.0"

In your main code:

mod message {
  #[derive(Debug, Serialize, Deserialize)]
  struct Host {
    ...
  }

  #[derive(Debug, Serialize, Deserialize)]
  struct Worker { 
    ...
  }
}

// ...

let worker = Rc::new(Worker::new("./worker.js").unwrap());
let mut transport: Transport<_, Codec, message::Worker, message::Host> =
        Transport::new(&worker, Codec::default()).await.except("failed to open transport");

use mezzenger::Receive;
let received = transport.receive().await.except("failed to receive message");

let message = message::Host { ... };
transport.send(&message).await.except("failed to send message");

In Web Worker:

mod message {
  #[derive(Debug, Serialize, Deserialize)]
  struct Host {
    ...
  }

  #[derive(Debug, Serialize, Deserialize)]
  struct Worker { 
    ...
  }
}

// ...

let mut transport: Transport<_, Codec, message::Host, message::Worker> =
        Transport::new_in_worker(Codec::default()).await.except("failed to open transport");

let message = message::Worker { ... };
transport.send(&message).await.except("failed to send message");

use mezzenger::Receive;
let received = transport.receive().await.except("failed to receive message");

See rust-webapp-template for more comprehensive example.

see also

mezzenger

rust-webapp-template

Using Web Workers

WASM in Web Worker

Commit count: 191

cargo fmt