workers-rs/examples/emscripten-tcp
Guy Bedford df2a249691
worker-build: Emscripten 7 and the Tokio tick event loop (#1135)
* worker-build: update emscripten builds to Emscripten 7 and the Tokio tick event loop

Tokio's hosted event loop (tokio-rs/tokio#8484) now waits on the epoll
readiness listeners released in Emscripten 6.0.12, and wasm-bindgen drives
it through LocalEventLoop::tick. The pin moves to 7.0.0, dropping the
listener and source map backports it contains, and listener callbacks are
delivered as a microtask of the notifying task so that concurrent inbound
connections are each served in their own request.

* examples: update the tokio patchset tag commit

* Update wasm-bindgen to the EventLoop tokio API (wasm-bindgen#5467)
History 2026-11-11 16:24:15 -08:00
..
src worker-build --emscripten (#1061) 2026-09-25 16:27:59 -07:00
.gitignore worker-build --emscripten (#1061) 2026-09-25 16:27:59 -07:00
Cargo.lock worker-build: Emscripten 7 and the Tokio tick event loop (#1135) 2026-11-11 16:24:15 -08:00
Cargo.toml worker-build: Emscripten 7 and the Tokio tick event loop (#1135) 2026-11-11 16:24:15 -08:00
README.md worker-build: Emscripten 7 and the Tokio tick event loop (#1135) 2026-11-11 16:24:15 -08:00
rust-toolchain.toml worker-build --emscripten (#1061) 2026-09-25 16:27:59 -07:00
wrangler.toml worker-build --emscripten (#1061) 2026-09-25 16:27:59 -07:00

Emscripten TCP example

Experimental Preview

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

Raw TCP from a Worker with tokio::net, using the Emscripten Tokio patchset (the guybedford/tokio tag pinned in Cargo.toml, tokio-rs/tokio#8484). Building on the emscripten-tokio example, the Worker connects to port 80 of a host, sends an HTTP HEAD request and returns the reply.

npx wrangler dev
curl 'http://localhost:8787/?host=example.com'

TcpStream::connect((host, 80)), write_all and read_to_string are the ordinary Tokio API, under a tokio::time::timeout. The socket is backed by the Workers connect() API through Emscripten's -sNODERAWSOCKETS, and readiness flows through the same event loop that drives the handler. The hostname resolves through Emscripten's asynchronous getaddrinfo (emscripten#27742, applied by worker-build), which Tokio's ToSocketAddrs uses on this target.

What's Next

See the emscripten-incoming-tcp example for using a native accept to receive a TCP connection.