kotlinx.rpc 0.9.1 Help

Services

Services are the centerpieces of the library. A service is an interface annotated with the @Rpc annotation, and contains a set of methods that can be executed remotely.

A simple service can be declared as follows:

@Rpc interface MyService { suspend fun hello(name: String): String }

Here we declare the method hello, that accepts one argument and returns a string. The method is marked with a suspend modifier, which means that we use coroutines to send RPC requests.

To be able to use a service both in client and server code, the interface should be placed in the common module — kudos to Kotlin Multiplatform.

Now we can implement the service, so the server knows how to process incoming requests.

class MyServiceImpl : MyService { override suspend fun hello(name: String): String { return "Hello, $name! I'm server!" } }

The server will use that implementation to answer the client requests.

Streaming

The library supports streaming of data. To declare a streaming method, you need to use the Flow type. For example, to declare a method that returns a stream of integers:

@Rpc interface MyService { fun stream(): Flow<Int> }

Note that the method is not marked with the suspend modifier.

You can also pass a Flow as a parameter to the method. For example, to declare a method that accepts a stream of integers:

@Rpc interface MyService { suspend fun sendStream(stream: Flow<Int>) }

Or make streams bidirectional:

@Rpc interface MyService { fun bidi(stream: Flow<Int>): Flow<Int> }
Last modified: 13 June 2025