gloo-net

Crates.io version Download docs.rs docs

API Docs | Contributing | Chat

Built with 🦀🕸 by The Rust and WebAssembly Working Group

HTTP requests library for WASM Apps. It provides idiomatic Rust bindings for the web_sys fetch, WebSocket and EventSource APIs.

Examples

HTTP

rust let resp = Request::get("/path") .send() .await .unwrap(); assert_eq!(resp.status(), 200);

WebSocket

```rust use gloonet::websocket::{Message, futures::WebSocket}; use wasmbindgenfutures::spawnlocal; use futures::{SinkExt, StreamExt};

let mut ws = WebSocket::open("wss://echo.websocket.org").unwrap(); let (mut write, mut read) = ws.split();

spawn_local(async move { write.send(Message::Text(String::from("test"))).await.unwrap(); write.send(Message::Text(String::from("test 2"))).await.unwrap(); });

spawnlocal(async move { while let Some(msg) = read.next().await { consolelog!(format!("1. {:?}", msg)) } console_log!("WebSocket Closed") }) ```

EventSource

```rust use gloonet::eventsource::futures::EventSource; use wasmbindgenfutures::spawnlocal; use futures::{stream, StreamExt};

let mut es = EventSource::new("http://api.example.com/ssedemo.php").unwrap(); let stream1 = es.subscribe("some-event-type").unwrap(); let stream2 = es.subscribe("another-event-type").unwrap();

spawnlocal(async move { let mut allstreams = stream::select(stream1, stream2); while let Some(Ok((eventtype, msg))) = allstreams.next().await { consolelog!(format!("1. {}: {:?}", eventtype, msg)) } console_log!("EventSource Closed"); }) ```