ServerSSESessionWithSerialization

Represents a server-side Server-Sent Events (SSE) session with serialization support. An ServerSSESessionWithSerialization allows the server to send ServerSentEvent to the client over a single HTTP connection.

Example of usage:

install(SSE)
routing {
    sse("/serialization", serialize = { typeInfo, it ->
        val serializer = Json.serializersModule.serializer(typeInfo.kotlinType!!)
        Json.encodeToString(serializer, it)
    }) {
        send(Customer(0, "Jet", "Brains"))
        send(Product(0, listOf(100, 200)))
    }
}

To learn more, see the SSE and the SSE specification.

Report a problem

See also

Properties

Link copied to clipboard
abstract val call: ApplicationCall

The received call that originated this session.

Link copied to clipboard
Link copied to clipboard
abstract val serializer: (TypeInfo, Any) -> String

Serializer for transforming data object into field data of ServerSentEvent.

Functions

Link copied to clipboard
abstract suspend fun close()

Closes the ServerSSESession, terminating the connection with the client. Once this method is called, the SSE session is closed and no further events can be sent. You don't need to call this method as it is called automatically when all the send operations are completed.

Link copied to clipboard
fun ServerSSESession.heartbeat(heartbeatConfig: Heartbeat.() -> Unit = {})

Starts a heartbeat for the ServerSSESession.

Link copied to clipboard
abstract suspend fun send(event: ServerSentEvent)

Sends a ServerSentEvent to the client.

open suspend fun send(data: String? = null, event: String? = null, id: String? = null, retry: Long? = null, comments: String? = null)

Creates and sends a ServerSentEvent to the client.

Link copied to clipboard
inline suspend fun <T : Any> ServerSSESessionWithSerialization.send(data: T)
inline suspend fun <T : Any> ServerSSESessionWithSerialization.send(    data: T? = null,     event: String? = null,     id: String? = null,     retry: Long? = null,     comments: String? = null)