Skip to content

feature: SSE endpoints#4462

Draft
MukjepScarlet wants to merge 24 commits into
lysine-dev:trunkfrom
MukjepScarlet:feat_sse
Draft

feature: SSE endpoints#4462
MukjepScarlet wants to merge 24 commits into
lysine-dev:trunkfrom
MukjepScarlet:feat_sse

Conversation

@MukjepScarlet

@MukjepScarlet MukjepScarlet commented Aug 8, 2025

Copy link
Copy Markdown

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Collaborator

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

@MukjepScarlet

MukjepScarlet commented Aug 11, 2025

Copy link
Copy Markdown
Author

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
Copy Markdown
Author

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

Now it requires the default callback style adapter.
@MukjepScarlet

Copy link
Copy Markdown
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

Development

Successfully merging this pull request may close these issues.

Support for server sent events

2 participants