Giter Club home page Giter Club logo

eventsource-stream's Introduction

eventsource-stream

A basic building block for building an Eventsource from a Stream of bytes array like objects. To learn more about Server Sent Events (SSE) take a look at the MDN docs

Example

let mut stream = reqwest::Client::new()
    .get("http://localhost:7020/notifications")
    .send()
    .await?
    .bytes_stream()
    .eventsource();

while let Some(thing) = stream.next().await {
   println!("{:?}", thing);
}

License: MIT OR Apache-2.0

eventsource-stream's People

Contributors

dunglas avatar jpopesculian avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

eventsource-stream's Issues

Message without `data` field gets ignored

Hi,

if event.data.is_empty() {
return None;
}

If a SSE message arrive without data field, it is silently ignored.

I guess this is due to this rule (cf. https://html.spec.whatwg.org/multipage/server-sent-events.html#dispatchMessage):

If the data buffer is an empty string, set the data buffer and the event type buffer to the empty string and return.

However it is also specified:

When the user agent is required to dispatch the event, the user agent must process the data buffer, the event type buffer, and the last event ID buffer using steps appropriate for the user agent.

For web browsers, the appropriate steps to dispatch the event are as follows:

So my understanding is this behavior is not mandatory in the spec, on the other hand it is error prone:

  • when implementing retry on server-side, it is tempting to just send a retry:1000\n\n message
  • when implementing custom event on sever-side, the obvious way is to return them without data if they don't need it. For instance event:missed_events\n\n to signify to the client it Last-Event-ID header was too old and hasn't been respected.

I don't think modifying the behavior of this lib is needed though (as it would make it incompatible with what does regular web browsers). This is more like a poor decision when the SSE specification has been written resulting in a footgun.

However I think it's relevant to document this quirk (being in the project's documentation, or just by having this issue) to save time for everybody ;-)

Fails To Parse Event

When trying to listen for eventsource from couchdb changes feed , it fails to parse event withh error invalid field.
`data: {"seq":"1-g1AAAAFHeJy1TssKwjAQXK0gfkwXFRGP_QE_YrO7EsuaSNt4bf88Rqk3j3qZYQbmYQCw9JXAmWNiL66Zub53UeqP2OE3G0UvlGzA_sHIlvpBO7TIZPaqJXBjzrn1FcGtGOsTER3dQWCTQoleg8qfpsOqIEyF8vxg8X4gxFvd66832yeC93F0","id":"f48b4a4a-d449-409e-9e2f-adc79ac3db4f","changes":[{"rev":"1-8751ebdb500d588544dda38a6f9f317a"}]}
id: "1-g1AAAAFHeJy1TssKwjAQXK0gfkwXFRGP_QE_YrO7EsuaSNt4bf88Rqk3j3qZYQbmYQCw9JXAmWNiL66Zub53UeqP2OE3G0UvlGzA_sHIlvpBO7TIZPaqJXBjzrn1FcGtGOsTER3dQWCTQoleg8qfpsOqIEyF8vxg8X4gxFvd66832yeC93F0"

data: {"seq":"2-g1AAAAFHeJy1TssKwjAQXFpB_JguKiIe_YF-xGZ3JZY1kbbxav88jVJvHvUywwzMwwCg8rVAyzGxF3deuLn3UZqP2OE3G0UvlGzE4cHIloZRe7TIZPaqJXDPnHPna4JbMdYnIjq6g8AmhRK9BpU_TYdVQZgK5eVB9X4gxFvd6683uxmDVXF1","id":"07ebd855-81cf-4737-8034-6a200a5041b8","changes":[{"rev":"1-0194907cb383fe7e9e32c7874bff16dd"}]}
id: "2-g1AAAAFHeJy1TssKwjAQXFpB_JguKiIe_YF-xGZ3JZY1kbbxav88jVJvHvUywwzMwwCg8rVAyzGxF3deuLn3UZqP2OE3G0UvlGzE4cHIloZRe7TIZPaqJXDPnHPna4JbMdYnIjq6g8AmhRK9BpU_TYdVQZgK5eVB9X4gxFvd6683uxmDVXF1"
`

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.