Get started
Edit pageLast modified: 10 February 2025kotlinx.rpc
is a multiplatform Kotlin library that provides its users with tools to perform Remote Procedure Calls (RPC) using Kotlin language constructs with as easy setup as possible. The library is transport agnostic by design, so most transfer mechanisms can be used under the hood: WebSockets, HTTP Requests, gRPC, etc.
tip
If you are not familiar with RPC, we strongly recommend reading Wikipedia's article on RPC before proceeding with this document.
What this library is and what it is not
It is important to understand that this library is a set of tools and APIs gathered in one place with a goal of providing a great user experience when working with RPC systems in Kotlin Multiplatform projects. kotlinx.rpc
provides its own in-house new RPC protocol, but it is not a library solely focused on this protocol.
The combination of Kotlin Multiplatform technology and RPC concept opens an opportunity to provide exceptional user experience while creating client-server applications, and we will create technologies that will embrace the concept in this library. This will include our own in-house RPC protocol with the focus on KMP projects, as well as integrations with other technologies, including but not limited to gRPC.
IDE Plugin
Library uses a Kotlin compiler plugin to generate declarations. To make this work in Intellij-based IDEs, we provide Kotlin External FIR Support plugin.
To learn more, see IDE Plugin page
Installation
Before adding kotlinx.rpc
dependencies, you need to configure your project with Gradle.
Configure the repositories
To be able to add dependencies for kotlinx.rpc
artifacts, you need to define a repository from which they will be consumed.
Add the following repository in your build.gradle.kts file:
repositories {
mavenCentral()
}
Add runtime dependencies
kotlinx.rpc
provides you with runtime dependencies (also called artifacts). To use these dependencies, you first need to define them in your project's build.gradle.kts file:
dependencies {
// example kotlinx.rpc artifacts
implementation("org.jetbrains.kotlinx:kotlinx-rpc-krpc-client:0.5.1")
implementation("org.jetbrains.kotlinx:kotlinx-rpc-krpc-server:0.5.1")
}
This adds the APIs needed to work with both client and server code using kotlinx.rpc
.
For a full example, see the single-module Ktor app example.
kotlin {
// source sets can be any other supported, ios and jvm are chosen just for an example
sourceSets {
iosMain {
// let's say that we have code for the client in iosMain sources set
implementation("org.jetbrains.kotlinx:kotlinx-rpc-krpc-client:0.5.1")
}
jvmMain {
// let's say that we have code for the server in jvmMain sources set
implementation("org.jetbrains.kotlinx:kotlinx-rpc-krpc-server:0.5.1")
}
}
}
Here we define dependencies for iosMain
and jvmMain
source sets, but it can be any other source set that you may want to have. Also, you may want to split your application into several modules, so that you have a server in one module and a client in another.
For a full example, see the Ktor web app example
Add plugin dependencies
To add a Gradle plugin to your project, you need to define the following in your build.gradle.kts:
plugins {
id("org.jetbrains.kotlinx.rpc.plugin") version "0.5.1"
}
This will configure code generation for your project.
Add serialization dependency
kotlinx.rpc
requires you to add the kotlinx.serialization Gradle plugin to your project.
plugins {
kotlin("jvm") version "2.1.10"
kotlin("plugin.serialization") version "2.1.10"
}
To learn how to configure serialization in kotlinx.rpc
, see
serialization DSL
.