WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Conversation

@MukjepScarlet
Copy link

@MukjepScarlet MukjepScarlet commented Aug 8, 2025

Prototype (my demand):

@SSE
@GET("events")
suspend fun fetchEvents(): Flow<ServerSentEvent>

data class ServerSentEvent(
    val id: String?,
    val type: String?
    val data: String,
)

Pending:

  1. Should it be migrated into base module? (e.g. @SSE can replace @GET)
  2. What kind of result should be supported?
  3. Write in Kotlin or Java?
  4. Auto-reconnecting like JS? Or improve okhttp-sse module.
  5. How can we handle connection events like open/disconnected here?
  6. How to handle id, type, data for example Int? for id, enum for type and JSON data?

java.util.concurrent.Flow (Java 9+) / java.util.stream.Stream (Java 8+) might be the integrated ones... But Stream is not designed for tasks like this.

These questions need to be answered before actual works.


closes #1029

@MukjepScarlet MukjepScarlet marked this pull request as draft August 8, 2025 08:32
@JakeWharton
Copy link
Collaborator

I think this can probably be entirely implemented in a call adapter with no changes to the core, but I'd have to try it.

@MukjepScarlet
Copy link
Author

We might have more than one adapter for SSE (for example RxJava & Kotlinx coroutines), so the basic definition like ServerSentEvent (or ServerSentEvent<T>) probably need to be in the core module.

@JakeWharton
Copy link
Collaborator

Call adapters can delegate to each other. They do not need to be in the core.

@MukjepScarlet
Copy link
Author

MukjepScarlet commented Aug 11, 2025

I planned to add a callback style for core module but it requires some improvements on okhttp-sse, another PR for that will be opened later.

@MukjepScarlet
Copy link
Author

@JakeWharton Please review current work of this for further change/improvement/completion.

Now it requires the default callback style adapter.
@MukjepScarlet
Copy link
Author

Current usage:

@Streaming
@GET
fun ktxFlow(): Flow<ServerSentEvent<String, String, String>>

@Streaming
@GET
fun jucFlow(): Flow.Publisher<ServerSentEvent<String, String, String>>

@Streaming
@GET
fun callback(): EventSource<String, String, String>

The fields of ServerSentEvent will be converted as a String ResponseBody.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support for server sent events

2 participants