Available on crate feature eventsource only.
Expand description

A wrapper around the EventSource API using the Futures API to be used with async rust.

EventSource is similar to WebSocket with the major differences being:

  • they are a one-way stream of server generated events
  • their connection is managed entirely by the browser
  • their data is slightly more structured including an id, type and data

EventSource is therefore suitable for simpler scenarios than WebSocket.

See the MDN Documentation to learn more.

Example

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");
})

Structs

Wrapper around browser’s EventSource API. Dropping this will close the underlying event source.
Wrapper around browser’s EventSource API.