ianaio-net

Crates.ioianaio-net
lib.rsianaio-net
version0.1.0
sourcesrc
created_at2024-06-08 07:49:01.850842
updated_at2024-06-08 07:49:01.850842
descriptionHTTP requests library for WASM Apps
homepagehttps://rustwasm.iana.io/net
repositoryhttps://github.com/rustwasm/ianaio
max_upload_size
id1265557
size95,798
ianaio (ianaio)

documentation

https://docs.iana.io/rustwasm/net

README

ianaio-net

Crates.io version Download docs.rs docs

API Docs | Contributing | Chat

Built with 🦀🕸 by The IanaIO 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

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

WebSocket

use ianaio_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 ianaio_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");
})
Commit count: 0

cargo fmt