Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server Sent Event support #1346

Open
Th3-M4jor opened this issue Jun 22, 2020 · 2 comments
Open

Server Sent Event support #1346

Th3-M4jor opened this issue Jun 22, 2020 · 2 comments

Comments

@Th3-M4jor
Copy link

@Th3-M4jor Th3-M4jor commented Jun 22, 2020

Describe the feature you'd like
Adding a yew::service for Server Sent Events, I imagine this one wouldn't be too difficult to add as it would behave similar to a one-way Websocket, and web_sys has support for the EventSource object.

Describe alternatives you've considered (Optional)
While this can handled with Websockets as an alternative, the use cases are somewhat different, owing to communication being uni-directional.

Questionnaire

  • I'm interested in implementing this myself but don't know where to start
  • I would like to add this feature
  • I don't have time to add this right now, but maybe later
@liquidblock
Copy link
Contributor

@liquidblock liquidblock commented Jul 6, 2020

I was in need of server send events as well. Therefore i created this little library: https://github.com/liquidblock/yew-sse
Unfortunately i did not test nor document it (and i did not create an example), but used it like this:

match self.sse.open(
    &url,
    self.link.callback(move |(id, message): (String, String)| {
        Message::SendEventMessage(id, message)
    }),
    self.link.callback(move |value: EventSourceUpdate| {
        match value {
            EventSourceUpdate::Open => Message::SendEventOpen,
            EventSourceUpdate::Error => Message::SendEventError,
        }
    }),
) {
    Ok(task) => {
        self.task = Some(task);
    }
    Err(e) => {
        // error handling
    }
}

self.sse is of type yew_sse::services::sse::EventSourceService and initialized with EventSourceService::new().
self.task is of type Option<yew_sse::services::sse::EventSourceTask> and when dropped it will close the server send event handling.
The onopen, onerror and onmessage events are handled and also the last event ID together with the message is passed to the callback. But i did not implement named events since i did not need them, but was thinking about using a HashMap with the name of the event as a key and the event handler task as a value. With that it would be possible to add and remove named events.
I wrote this library to be used by myself, but also intended to be used with yew and was planning on creating a PR after adding an example, documentation and tests, but never got the time for it.

The library works with stdweb and web-sys, but it was written for yew version 0.16.2.

@thedanvail
Copy link

@thedanvail thedanvail commented Sep 22, 2020

I'm going to start taking a look at this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.