gRPC for Kotlin: Libraries, Server, and Client — Code Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

gRPC for Kotlin: Libraries and Examples



gRPC is a high-performance remote procedure call (RPC) framework developed by Google. It uses Protocol Buffers (protobuf) for data serialization and HTTP/2 for transport. In the Kotlin ecosystem, gRPC integrates seamlessly with coroutines, making it an ideal choice for building asynchronous microservices.



In this article, we will walk through setting up a gRPC server and client in Kotlin using the official gRPC-Kotlin (grpc-kotlin) library. You will see the full cycle: from describing the proto file to launching and testing.



1. Core Libraries



The following key components are used for working with gRPC in Kotlin:



  • protobuf-gradle-plugin — a plugin for compiling .proto files into Kotlin/Java classes.
  • grpc-kotlin-stub — a library that generates Kotlin coroutine stubs (suspend functions).
  • grpc-netty-shaded — a transport layer based on Netty (built-in HTTP/2).
  • grpc-protobuf-lite — a lightweight version of protobuf for Android/clients.


2. Project Structure



We will use Gradle (Kotlin DSL). Create a project with the following structure:



my-grpc-app/├── build.gradle.kts├── settings.gradle.kts└── src/    ├── main/    │   ├── proto/    │   │   └── hello.proto    │   └── kotlin/    │       ├── server/    │       │   └── HelloServer.kt    │       └── client/    │           └── HelloClient.kt


3. Proto File Description



Create hello.proto in src/main/proto/:



syntax = "proto3";

package com.example.grpc;

service Greeter { rpc SayHello (HelloRequest) returns (HelloReply);}

message HelloRequest { string name = 1;}

message HelloReply { string message = 1;}


4. Gradle Configuration (build.gradle.kts)



Add the necessary plugins and dependencies:



plugins {    id("org.jetbrains.kotlin.jvm") version "1.9.22"    id("com.google.protobuf") version "0.9.4"}

repositories { mavenCentral()}

dependencies { implementation("io.grpc:grpc-kotlin-stub:1.4.1") implementation("io.grpc:grpc-netty-shaded:1.60.0") implementation("io.grpc:grpc-protobuf-lite:1.60.0")}

protobuf { protoc { artifact = "com.google.protobuf:protoc:3.25.1" } plugins { create("grpc") { artifact = "io.grpc:protoc-gen-grpc-java:1.60.0" } create("grpckt") { artifact = "io.grpc:protoc-gen-grpc-kotlin:1.4.1:jdk8@jar" } } generateProtoTasks { all().forEach { task -> task.plugins { create("grpc") { } create("grpckt") { } } } }}


5. Implementing the gRPC Server



Create the file HelloServer.kt:



package com.example.server

import com.example.grpc.GreeterGrpcKtimport com.example.grpc.HelloReplyimport com.example.grpc.HelloRequestimport io.grpc.ServerBuilder

class GreeterService : GreeterGrpcKt.GreeterCoroutineImplBase() { override suspend fun sayHello(request: HelloRequest): HelloReply { println("Received request from: ${request.name}") return HelloReply.newBuilder() .setMessage("Hello, ${request.name}!") .build() }}

fun main() { val port = 50051 val server = ServerBuilder.forPort(port) .addService(GreeterService()) .build() .start()

println("gRPC server started on port $port") server.awaitTermination()}


6. Implementing the gRPC Client



Create the file HelloClient.kt:



package com.example.client

import com

Recommendations