Skip to content

Live events (SSE)

mero-kotlin streams a node’s real-time context events over Server-Sent Events, exposed as a Kotlin Flow. The task-oriented walkthrough is the live events guide; this page is the API surface.

fun events(contextIds: List<String>): Flow<ContextEvent>

Opens GET /sse and subscribes to the given context ids via POST /sse/subscription, then emits a ContextEvent per node emission. The returned Flow is cold — collecting it opens the connection; cancelling the collecting coroutine closes it. It reconnects automatically ~3s after a drop and re-subscribes (the node persists session subscriptions).

val job = scope.launch {
mero.events(listOf(contextId)).collect { event ->
println("${event.contextId} · ${event.kind}")
}
}
// later:
job.cancel() // closes the SSE stream
data class ContextEvent(
val contextId: String,
val kind: String, // event discriminator, e.g. "ExecutionEvent"
val payload: JsonElement, // the raw event JSON
)

For a contract emission (kind == "ExecutionEvent") the encoded contract events live under payload’s data.events[].data.

mero.events(...) builds an SseClient for you; use it directly only to manage the connection yourself.

class SseClient(
baseUrl: String,
token: suspend () -> String?,
client: OkHttpClient = OkHttpClient(),
json: Json = Json { ignoreUnknownKeys = true },
) {
fun events(contextIds: List<String>): Flow<ContextEvent>
}