Skip to content

Latest commit

 

History

History

emscripten-tokio

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

Emscripten Tokio example

Experimental Preview

git clone https://github.com/cloudflare/workers-rs
cd workers-rs/examples/emscripten-tokio
npx wrangler dev

This uses an experimental_tokio Tokio event loop on wasm-bindgen to support Tokio inside of workers.

While upstream PRs are still in progress, a Tokio patchset is needed to run this example, which is contained in Cargo.toml.

With these, every #[event] and #[durable_object] handler runs on its own Tokio event loop whose wait is the Workers event loop. There is no #[tokio::main] and no runtime to build: tokio::spawn, tokio::time, tokio::sync and tokio::join! work inside a plain async fn handler as they do natively, with no stack switching.

Worker-build also includes an Emscripten patch for a non-blocking epoll implementation which is auto applied when worker-build installs Emscripten under --emscripten, and passes the unstable cfgs both crates gate this behind (tokio_unstable, wasm_bindgen_unstable_tokio).

npx wrangler dev

Then try some of the example routes demonstrating Tokio functionality:

Route Shows
curl http://localhost:8787/spawn tokio::spawn and awaiting JoinHandles
curl http://localhost:8787/sleep?ms=100 tokio::time::sleep
curl http://localhost:8787/timeout tokio::time::timeout completing and elapsing
curl http://localhost:8787/channels mpsc between spawned producers and the handler
curl http://localhost:8787/mutex tokio::sync::Mutex shared across tasks
curl http://localhost:8787/join tokio::join! over concurrent futures

Each response carries the measured elapsed_ms, showing the work overlapping.

What's Next

See the emscripten-tcp example for a full TCP sockets app on workers with Emscripten and Tokio.