sseSession

suspend fun HttpClient.sseSession(    reconnectionTime: Duration? = null,     showCommentEvents: Boolean? = null,     showRetryEvents: Boolean? = null,     block: HttpRequestBuilder.() -> Unit): ClientSSESession
suspend fun HttpClient.sseSession(    scheme: String? = null,     host: String? = null,     port: Int? = null,     path: String? = null,     reconnectionTime: Duration? = null,     showCommentEvents: Boolean? = null,     showRetryEvents: Boolean? = null,     block: HttpRequestBuilder.() -> Unit = {}): ClientSSESession
suspend fun HttpClient.sseSession(    urlString: String,     reconnectionTime: Duration? = null,     showCommentEvents: Boolean? = null,     showRetryEvents: Boolean? = null,     block: HttpRequestBuilder.() -> Unit = {}): ClientSSESession

Opens a ClientSSESession to receive Server-Sent Events (SSE) from a server.

Report a problem

Parameters

reconnectionTime

The time duration to wait before attempting reconnection in case of connection loss

showCommentEvents

When enabled, events containing only comments field will be presented in the incoming flow

showRetryEvents

When enabled, events containing only comments field will be presented in the incoming flow

Example of usage:

val session = client.sseSession {
    url("http://localhost:8080/sse")
}
session.incoming.collect { event ->
    println("Id: ${event.id}")
    println("Event: ${event.event}")
    println("Data: ${event.data}")
}

suspend fun HttpClient.sseSession(    deserialize: (TypeInfo, String) -> Any?,     reconnectionTime: Duration? = null,     showCommentEvents: Boolean? = null,     showRetryEvents: Boolean? = null,     block: HttpRequestBuilder.() -> Unit): ClientSSESessionWithDeserialization
suspend fun HttpClient.sseSession(    scheme: String? = null,     host: String? = null,     port: Int? = null,     path: String? = null,     deserialize: (TypeInfo, String) -> Any?,     reconnectionTime: Duration? = null,     showCommentEvents: Boolean? = null,     showRetryEvents: Boolean? = null,     block: HttpRequestBuilder.() -> Unit = {}): ClientSSESessionWithDeserialization
suspend fun HttpClient.sseSession(    urlString: String,     deserialize: (TypeInfo, String) -> Any?,     reconnectionTime: Duration? = null,     showCommentEvents: Boolean? = null,     showRetryEvents: Boolean? = null,     block: HttpRequestBuilder.() -> Unit = {}): ClientSSESessionWithDeserialization

Opens a ClientSSESessionWithDeserialization to receive Server-Sent Events (SSE) from a server with ability to deserialize the data field of the TypedServerSentEvent and performs block.

Report a problem

Parameters

deserialize

The deserializer function to transform the data field of the TypedServerSentEvent into an object

reconnectionTime

The time duration to wait before attempting reconnection in case of connection loss

showCommentEvents

When enabled, events containing only comments field will be presented in the incoming flow

showRetryEvents

When enabled, events containing only comments field will be presented in the incoming flow

Example of usage:

val session = client.sseSession("http://localhost:8080/sse", deserialize = { typeInfo, jsonString ->
    val serializer = Json.serializersModule.serializer(typeInfo.kotlinType!!)
    Json.decodeFromString(serializer, jsonString)!!
})

session.apply {
    incoming.collect { event: TypedServerSentEvent<String> ->
        when (event.event) {
            "customer" -> {
                val customer: Customer? = deserialize<Customer>(event.data)
            }

            "product" -> {
                val product: Product? = deserialize<Product>(event.data)
            }
        }
    }
}