Extensible Kotlin Multiplatform implementation of Server Sent Events(Sse)
// build.gradle.kts
//...
dependencies {
// stream provider, the SSE event source is a transitive dependency
implementation("cc.scrambledbytes.sse:ktor-stream-provider:1.0.0")
// client engine
implementation("io.ktor:ktor-client-apache5:2.3.0")
}
// in a suspend function
val http = HttpClient()
val provider: SseLineStream.Provider = KtorSseEventStreamProvider(http)
val client = SseEventSource(
url = "https://0.0.0.0:8080/sse",
streamProvider = provider,
)
client.open()
client.events.collect {
println("[Client] Got event: $it")
}
- Ktor
You can easily write your own stream provider by implementing the SseLineStream.Provider
interface.
An SseLineStream.Provider
needs to create a SseLineStream
which needs two lambdas:
onClose
: which is executed when the connection is closed by the sourceonConnect
: which is executed when a connection to the SSE source is being attempted. It passes two callback handles,onConnect
andonLine
.onConnect
needs to be called exactly once after the connection has been initialized, andonLine
needs to be called everytime a line has arrived in the text stream.
In addition to that, a SseLineStream
provider should set the following headers/attributes for each connection attempt:
- CacheControl: no-store
- Accept: text/event-stream
- intitator type: other
- (optional, if true) withCredentials: include
- (optional, if not null) Last-Event-ID : lastEventId
Additional requirements:
- received content lines should be decoded as UTF-8 strings
- The provider may decide when a request is failed in the
onConnect
callback
Apache 2.0
- fix kmp targets
- github actions
- more tests
- static code analysis
- add providers
- documentation