sse

fun Route.sse(path: String, handler: suspend ServerSSESession.() -> Unit)(source)

Adds a route to handle Server-Sent Events (SSE) at the specified path using the provided handler. Requires SSE plugin to be installed.

Report a problem

Parameters

path

A URL path at which to handle Server-Sent Events (SSE) requests.

handler

A function that defines the behavior of the SSE session. It is invoked when a client connects to the SSE endpoint. Inside the handler, you can use the functions provided by ServerSSESessionWithSerialization to send events to the connected clients.

Example of usage:

install(SSE)
routing {
sse("/events") {
repeat(100) {
send(ServerSentEvent("event $it"))
}
}
}

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

See also


fun Route.sse(handler: suspend ServerSSESession.() -> Unit)(source)

Adds a route to handle Server-Sent Events (SSE) using the provided handler. Requires SSE plugin to be installed.

Report a problem

Parameters

handler

A function that defines the behavior of the SSE session. It is invoked when a client connects to the SSE endpoint. Inside the handler, you can use the functions provided by ServerSSESessionWithSerialization to send events to the connected clients.

Example of usage:

install(SSE)
routing {
sse {
repeat(100) {
send(ServerSentEvent("event $it"))
}
}
}

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

See also


fun Route.sse(path: String, serialize: (TypeInfo, Any) -> String, handler: suspend ServerSSESessionWithSerialization.() -> Unit)(source)

Adds a route to handle Server-Sent Events (SSE) at the specified path using the provided handler. Requires SSE plugin to be installed.

Report a problem

Parameters

path

A URL path at which to handle Server-Sent Events (SSE) requests.

serialize

A function to serialize data objects into the data field of a ServerSentEvent.

handler

A function that defines the behavior of the SSE session. It is invoked when a client connects to the SSE endpoint. Inside the handler, you can use the functions provided by ServerSSESessionWithSerialization to send events to the connected clients.

Example of usage:

install(SSE)
routing {
sse("/json", 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.

See also


fun Route.sse(serialize: (TypeInfo, Any) -> String, handler: suspend ServerSSESessionWithSerialization.() -> Unit)(source)

Adds a route to handle Server-Sent Events (SSE) using the provided handler. Requires SSE plugin to be installed.

Report a problem

Parameters

serialize

A function to serialize data objects into the data field of a ServerSentEvent.

handler

A function that defines the behavior of the SSE session. It is invoked when a client connects to the SSE endpoint. Inside the handler, you can use the functions provided by ServerSSESessionWithSerialization to send events to the connected clients.

Example of usage:

install(SSE)
routing {
sse(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.

See also