Skip to content

uakihir0/kgrpc

Repository files navigation

日本語

kgrpc

Maven Central Version

badge badge badge badge badge badge

This library is a gRPC client library compatible with Kotlin Multiplatform. It provides a common gRPC API across JVM, JavaScript, iOS, macOS, Linux, and Windows platforms. On JVM it uses grpc-java, on JavaScript it uses gRPC-Web over Ktor HttpClient, and on native platforms (iOS, macOS, Linux, Windows) it uses Rust/Tonic via FFI.

Platform Support

Platform Implementation Streaming Support
JVM grpc-java + grpc-kotlin-stub + grpc-okhttp All types
JS (IR) gRPC-Web over Ktor HttpClient Unary + Server-streaming
iOS / macOS Rust/Tonic FFI via cinterop All types
Linux x64 Rust/Tonic FFI via cinterop All types
Windows mingwX64 Rust/Tonic FFI via cinterop All types

Usage

Below is how to use it with Kotlin on the supported platforms using Gradle. If you are using it on an Apple platform, please refer to kgrpc-spm or kgrpc-cocoapods. Additionally, please check the test code as well.

Stable (Maven Central)

repositories {
    mavenCentral()
}

dependencies {
+   implementation("work.socialhub.kgrpc:core:0.0.1")
}

Snapshot

repositories {
+   maven { url = uri("https://repo.repsy.io/mvn/uakihir0/public") }
}

dependencies {
+   implementation("work.socialhub.kgrpc:core:0.0.1-SNAPSHOT")
}

Using as part of a regular Java project

All of the above can be added to and used in regular Java projects, too. All you have to do is to use the suffix -jvm when listing the dependency.

Here is a sample Maven configuration:

<dependency>
    <groupId>work.socialhub.kgrpc</groupId>
    <artifactId>core-jvm</artifactId>
    <version>[VERSION]</version>
</dependency>

Creating a Channel

val channel = Channel.Builder
    .forAddress("localhost", 50051)
    .usePlaintext()
    .build()

Creating a Channel with TLS

val channel = Channel.Builder
    .forAddress("example.com", 443)
    .useTransportSecurity()
    .build()

Making a Unary RPC Call

val stub = MyServiceStub(channel)
val response = stub.myMethod(request)

Interceptors

val channel = Channel.Builder
    .forAddress("localhost", 50051)
    .usePlaintext()
    .intercept(object : CallInterceptor {
        override fun onSendHeaders(headers: Metadata): Metadata {
            headers.put("authorization", "Bearer $token")
            return headers
        }
    })
    .build()

Key Concepts

  • Channel - gRPC connection (configured via Channel.Builder)
  • Message / MessageCompanion - Protobuf message serialization/deserialization
  • Stub - Generated service stubs for making RPC calls
  • CallInterceptor - Intercept and modify RPC metadata, messages, and status

License

MIT License

Author

Akihiro Urushihara

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors