13 unstable releases (5 breaking)

0.6.0 Jul 21, 2024
0.5.0 Dec 6, 2023
0.4.0 Aug 11, 2023
0.3.1 Jul 24, 2023
0.1.0 Mar 11, 2022

#197 in WebAssembly

Download history 63504/week @ 2024-07-29 57299/week @ 2024-08-05 65061/week @ 2024-08-12 60937/week @ 2024-08-19 57955/week @ 2024-08-26 55910/week @ 2024-09-02 62628/week @ 2024-09-09 57509/week @ 2024-09-16 55003/week @ 2024-09-23 53489/week @ 2024-09-30 62873/week @ 2024-10-07 61469/week @ 2024-10-14 86460/week @ 2024-10-21 78836/week @ 2024-10-28 85925/week @ 2024-11-04 71504/week @ 2024-11-11

327,097 downloads per month
Used in 360 crates (43 directly)

MIT/Apache

84KB
1.5K SLoC

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

Examples

HTTP

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

WebSocket

use gloo_net::websocket::{Message, futures::WebSocket};
use wasm_bindgen_futures::spawn_local;
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();
});

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

EventSource

use gloo_net::eventsource::futures::EventSource;
use wasm_bindgen_futures::spawn_local;
use futures::{stream, StreamExt};

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

spawn_local(async move {
    let mut all_streams = stream::select(stream_1, stream_2);
    while let Some(Ok((event_type, msg))) = all_streams.next().await {
        console_log!(format!("1. {}: {:?}", event_type, msg))
    }
    console_log!("EventSource Closed");
})

Dependencies

~8–11MB
~196K SLoC