Skip to content

drice-buf/connect-kotlin

 
 

Repository files navigation

Build License

Connect-Kotlin

Connect-Kotlin is a slim library for using generated, type-safe, and idiomatic Kotlin clients to communicate with your app's servers using Protocol Buffers (Protobuf). It works with the Connect, gRPC, and gRPC-Web protocols.

Given a simple Protobuf schema, Connect-Kotlin generates idiomatic Kotlin protocol interfaces and client implementations:

Click to expand ElizaServiceClient.kt
public class ElizaServiceClient(
    private val client: ProtocolClientInterface
) : ElizaServiceClientInterface {
    public override suspend fun say(request: SayRequest, headers: Headers):
        ResponseMessage<SayResponse> = client.unary(
        request,
        headers,
        MethodSpec(
            "connectrpc.eliza.v1.ElizaService/Say",
            com.connectrpc.eliza.v1.SayRequest::class,
            com.connectrpc.eliza.v1.SayResponse::class
        )
    )

    public override suspend fun converse(headers: Headers):
        BidirectionalStreamInterface<ConverseRequest, ConverseResponse> = client.stream(
        headers,
        MethodSpec(
            "connectrpc.eliza.v1.ElizaService/Converse",
            com.connectrpc.eliza.v1.ConverseRequest::class,
            com.connectrpc.eliza.v1.ConverseResponse::class
        )
    )

    public override suspend fun introduce(headers: Headers):
        ServerOnlyStreamInterface<IntroduceRequest, IntroduceResponse> = client.serverStream(
        headers,
        MethodSpec(
            "connectrpc.eliza.v1.ElizaService/Introduce",
            com.connectrpc.eliza.v1.IntroduceRequest::class,
            com.connectrpc.eliza.v1.IntroduceResponse::class
        )
    )
}
Click to expand ElizaServiceClientInterface.kt
public interface ElizaServiceClientInterface {
    public suspend fun say(request: SayRequest, headers: Headers = emptyMap()):
        ResponseMessage<SayResponse>

    public suspend fun converse(headers: Headers = emptyMap()):
        BidirectionalStreamInterface<ConverseRequest, ConverseResponse>

    public suspend fun introduce(headers: Headers = emptyMap()):
        ServerOnlyStreamInterface<IntroduceRequest, IntroduceResponse>
}

This code can then be integrated with just a few lines:

class MainActivity : AppCompatActivity() {
    private lateinit var elizaServiceClient: ElizaServiceClientInterface

    private suspend fun say(sentence: String) {
        // Make a unary request to Eliza.
        val response = elizaServiceClient.say(SayRequest.newBuilder().setSentence(sentence).build())
        val elizaSentence = response.success { success ->
            // Get Eliza's reply from the response.
            success.message.sentence
        }
        // Use the elizaSentence in your views.
    }
}

That’s it! You no longer need to manually define request/response models, specify the exact path of your request, nor worry about the underlying networking transport for your applications!

Quick Start

Head over to our quick start tutorial to get started. It only takes ~10 minutes to complete a working chat app that uses Connect-Kotlin!

Documentation

Comprehensive documentation for everything, including interceptors, streaming, and error handling is available on the connectrpc.com website.

Generation Options

Option Type Default Details
generateCallbackMethods Boolean false Generate callback signatures for unary methods.
generateCoroutineMethods Boolean true Generate suspend signatures for unary methods.
generateBlockingUnaryMethods Boolean false Generate blocking signatures for unary methods.

Example Apps

Example apps are available in /examples. First, run make generate to generate code for the Protobuf plugins.

For the Android example, you can run make installandroid to build and install a fully functional Android application using Connect-Kotlin.

Additionally, there are pure Kotlin examples that demonstrate a simple main executable using Connect-Kotlin:

The examples demonstrates:

Contributing

We'd love your help making Connect better!

Extensive instructions for building the library and generator plugins locally, running tests, and contributing to the repository are available in our CONTRIBUTING.md guide. Please check it out for details.

Ecosystem

  • connect-swift: Swift clients for idiomatic gRPC & Connect RPC
  • connect-es: Type-safe APIs with Protobuf and TypeScript.
  • connect-go: Service handlers and clients for GoLang
  • Buf Studio: web UI for ad-hoc RPCs
  • conformance: Connect, gRPC, and gRPC-Web interoperability tests

Status

This project is in beta, and we may make a few changes as we gather feedback from early adopters. Join us on Slack!

Legal

Offered under the Apache 2 license.

About

The Kotlin implementation of Connect: Protobuf RPC that works.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Kotlin 98.5%
  • Makefile 1.5%