serverSentEvents

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

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

Note: ClientSSESession is bound to the session lifetime. Its scope is canceled when the serverSentEvents { ... } block returns or when the connection closes.

Example of usage:

client.serverSentEvents("http://localhost:8080/sse") { // `this` is `ClientSSESession`
incoming.collect { event ->
println("Id: ${event.id}")
println("Event: ${event.event}")
println("Data: ${event.data}")
}
}

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, retry directives (lines starting with retry:) are emitted as events

Report a problem


suspend fun HttpClient.serverSentEvents(request: HttpRequestBuilder.() -> Unit, deserialize: (TypeInfo, String) -> Any?, reconnectionTime: Duration? = null, showCommentEvents: Boolean? = null, showRetryEvents: Boolean? = null, block: suspend ClientSSESessionWithDeserialization.() -> Unit)(source)

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

Note: ClientSSESessionWithDeserialization is bound to the session lifetime. Its scope is canceled when the serverSentEvents { ... } block returns or when the connection closes.

Example of usage:

client.serverSentEvents({
url("http://localhost:8080/sse")
}, deserialize = {
typeInfo, jsonString ->
val serializer = Json.serializersModule.serializer(typeInfo.kotlinType!!)
Json.decodeFromString(serializer, jsonString)!!
}) { // `this` is `ClientSSESessionWithDeserialization`
incoming.collect { event: TypedServerSentEvent<String> ->
when (event.event) {
"customer" -> {
val customer: Customer? = deserialize<Customer>(event.data)
}
"product" -> {
val product: Product? = deserialize<Product>(event.data)
}
}
}
}

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, retry directives (lines starting with retry:) are emitted as events

Report a problem


suspend fun HttpClient.serverSentEvents(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, request: HttpRequestBuilder.() -> Unit = {}, block: suspend ClientSSESessionWithDeserialization.() -> Unit)(source)

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.

Note: ClientSSESessionWithDeserialization is bound to the session lifetime. Its scope is canceled when the serverSentEvents { ... } block returns or when the connection closes.

Example of usage:

client.serverSentEvents({
url("http://localhost:8080/sse")
}, deserialize = {
typeInfo, jsonString ->
val serializer = Json.serializersModule.serializer(typeInfo.kotlinType!!)
Json.decodeFromString(serializer, jsonString)!!
}) { // `this` is `ClientSSESessionWithDeserialization`
incoming.collect { event: TypedServerSentEvent<String> ->
when (event.event) {
"customer" -> {
val customer: Customer? = deserialize<Customer>(event.data)
}
"product" -> {
val product: Product? = deserialize<Product>(event.data)
}
}
}
}

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, retry directives (lines starting with retry:) are emitted as events

Report a problem


suspend fun HttpClient.serverSentEvents(urlString: String, deserialize: (TypeInfo, String) -> Any?, reconnectionTime: Duration? = null, showCommentEvents: Boolean? = null, showRetryEvents: Boolean? = null, request: HttpRequestBuilder.() -> Unit = {}, block: suspend ClientSSESessionWithDeserialization.() -> Unit)(source)

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.

Note: ClientSSESessionWithDeserialization is bound to the session lifetime. Its scope is canceled when the serverSentEvents { ... } block returns or when the connection closes.

Example of usage:

client.sse({
url("http://localhost:8080/serverSentEvents")
}, deserialize = {
typeInfo, jsonString ->
val serializer = Json.serializersModule.serializer(typeInfo.kotlinType!!)
Json.decodeFromString(serializer, jsonString)!!
}) { // `this` is `ClientSSESessionWithDeserialization`
incoming.collect { event: TypedServerSentEvent<String> ->
when (event.event) {
"customer" -> {
val customer: Customer? = deserialize<Customer>(event.data)
}
"product" -> {
val product: Product? = deserialize<Product>(event.data)
}
}
}
}

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, retry directives (lines starting with retry:) are emitted as events

Report a problem