kotlinx.rpc 0.2.1 Help

Configuration

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

val config: RPCConfig.Client = rpcClientConfig { // same for RPCConfig.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 RPCConfig.

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 RPCClient or RPCServer. 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: 13 June 2024