kotlinx.rpc 0.6.2 Help

Configuration

KrpcConfig is a class used to configure KrpcClient and KrpcServer (not to be confused with KrpcClient and KrpcServer). It has two children: KrpcConfig.Client and KrpcConfig.Server. Client and Server may have shared properties as well as distinct ones. To create instances of these configurations, DSL builders are provided (KrpcConfigBuilder.Client class with rpcClientConfig function and KrpcConfigBuilder.Server class with rpcServerConfig function respectively):

val config: KrpcConfig.Client = rpcClientConfig { // same for KrpcConfig.Server with rpcServerConfig waitForServices = true // default parameter }

The following configuration options are available:

serialization DSL

This parameter defines how serialization should work in RPC services and is present in both client and server configurations.

The serialization process is used to encode and decode data in RPC requests, so that the data can be transferred through the network.

Currently only StringFormat and BinaryFormat from kotlinx.serialization are supported, and by default you can choose from Json, Protobuf or Cbor formats:

rpcClientConfig { serialization { json { /* this: JsonBuilder from kotlinx.serialization */ } cbor { /* this: CborBuilder from kotlinx.serialization */ } protobuf { /* this: ProtobufBuilder from kotlinx.serialization */ } } }

Only last defined format will be used to serialize requests. If no format is specified, the error will be thrown. You can also define a custom format.

sharedFlowParameters DSL

rpcClientConfig { sharedFlowParameters { replay = 1 // default parameter extraBufferCapacity = 10 // default parameter onBufferOverflow = BufferOverflow.SUSPEND // default parameter } }

This parameter is needed to decode SharedFlow parameters received from a peer. MutableSharedFlow, the default function to create a SharedFlow instance, has the following signature:

fun <T> MutableSharedFlow( replay: Int = 0, extraBufferCapacity: Int = 0, onBufferOverflow: BufferOverflow = BufferOverflow.SUSPEND ): MutableSharedFlow<T> { /* ... */ }

It creates a SharedFlowImpl class that contains these parameters as properties, but this class in internal in kotlinx.coroutines and neither SharedFlow, nor MutableShatedFlow interfaces define these properties, which makes it impossible (at least for now) to send these properties from one endpoint to another. But instances of these flows when deserialized should be created somehow, so to overcome this - configuration parameter is created. Configuration builder allows defining these parameters and produces a builder function that is then placed into the KrpcConfig.

waitForServices DSL

waitForServices parameter is available for both client and server. It specifies the behavior for an endpoint in situations when the message for a service is received, but the service is not present in KrpcClient or KrpcServer. If set to true, the message will be stored in memory, otherwise, the error will be sent to a peer endpoint, saying that the message was not handled. Default value is true.

Last modified: 28 January 2025