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

EventSource.Event (SSE) can't be used to send comments / keep alive messages #9117

Open
an-tex opened this issue Mar 26, 2019 · 4 comments
Open

Comments

@an-tex
Copy link

an-tex commented Mar 26, 2019

Play Version (2.7.0)

API (Scala)

Expected Behavior

It should be possible to send comment events using play.api.libs.EventSource.Event (aka ServerSentEvents / SSE). These events start with a colon and can be used to keep the connection alive, e.g.:

: this is a keep alive message

data: some actual event with data

As described in https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Examples

This way you could keep a connection alive by e.g.:

Ok.chunked(source.via(EventSource.flow).keepAlive(50.seconds, () => heartbeatEvent)).as(ContentTypes.EVENT_STREAM)

Actual Behavior

The case class play.api.libs.EventSource.Event always formats the line with a data: prefix. Hence pure comments / heartbeats can't be send.

@marcospereira
Copy link
Member

marcospereira commented Apr 1, 2019

Hey @an-tex,

Yes, makes sense to support event comments, maybe as a new field in play.api.libs.EventSource.Event or even as its own type. For now, a possible workaround is to send events with event: heartbeat, for example:

val heartbeatEvent = Event(data = heartbeatData, name = Some("heartbeat"))
Ok.chunked(source.via(EventSource.flow).keepAlive(50.seconds, () => heartbeatEvent)).as(ContentTypes.EVENT_STREAM)

And on the client side have an event listener for heartbeat.

@an-tex
Copy link
Author

an-tex commented Apr 4, 2019

Yep that workaround works. I'll try to squeeze in a PR over the next weeks.

@fusuiyi123
Copy link
Contributor

Hey @marcospereira since I don't see any further updates in this issue, may I work on this? Any pointer would be helpful thanks!

@an-tex
Copy link
Author

an-tex commented Apr 4, 2020

Hi @fusuiyi123 , sorry I totally forgot about that. From my side feel free to take over. Cheers

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

No branches or pull requests

3 participants