RpcBidiService

A service whose methods accept sub-service inputs, use Flow<T>, or otherwise require bidirectional transport capability.

Extend this instead of RpcHostService when any @KsMethod accepts a @KsService type as input, or uses Flow<T> (which internally bridges through a bidirectional sub-service protocol).

Only bidirectional transports (WebSocket, raw sockets, JNI) can host RpcBidiService instances.

Samples

// A @KsMethod can accept another @KsService type as input. The caller
// provides a service implementation that the server calls back into.
val producer = object : EventProducer {
    override suspend fun subscribe(consumer: EventConsumer): String {
        // The server calls back into the client-provided consumer.
        consumer.onEvent("connected")
        consumer.onEvent("data-ready")
        return "subscription-123"
    }
}

val env = ksrpcEnvironment { }
val serialized = producer.serialized(env)
val stub = serialized.toStub<EventProducer, String>()

// The client provides a callback implementation.
val callback = object : EventConsumer {
    override suspend fun onEvent(event: String) {
        println("Received event: $event")
    }
}
val subscriptionId = stub.subscribe(callback)

Functions

Link copied to clipboard
open suspend override fun close()

Called when the interaction with this object is done and its resources can be cleaned up.