Skip to content

scrambledbytes/kmp-sse

Repository files navigation

Kotlin Multiplatform SSE

Extensible Kotlin Multiplatform implementation of Server Sent Events(Sse)

Overview

Documentation

Setup:

// 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")
}

Basic Usage

// 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")
}

Supported SseLineStream providers

  • Ktor

Writing your own SseLineStream provider

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 source
  • onConnect: which is executed when a connection to the SSE source is being attempted. It passes two callback handles, onConnect and onLine. onConnect needs to be called exactly once after the connection has been initialized, and onLine 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:

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

License

Apache 2.0

Resources

Todo

  • fix kmp targets
  • github actions
  • more tests
  • static code analysis
  • add providers
  • documentation

About

SSE implmentation for Kotlin Multiplatform

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published