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, // "StateMutation" | "SyncStatus" | …
val payload: JsonElement, // the raw event JSON
)

The discriminator is the frame’s result.type. A live merod 0.11.0-rc.32 sends StateMutation (the context state moved) and SyncStatus (syncing / waitingForPeers / …). It does not send ExecutionEvent, which earlier versions of this page named — a when branching on it never fired.

For a contract emission (kind == "StateMutation") the encoded contract events live under payload’s data.events[].data, each with its own kind.

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>
}