When writing software, engineers often make mistakes implementing an agreed upon API by incorrectly defining the data structure, misnaming a key, or even specifying the wrong endpoint path. These errors make collaborating across iOS, Android, frontend, and backend difficult as they necessitate back-and-forth coordination and exhaustive testing. Connect’s goal is to eliminate these hardships by using a single Protobuf schema to generate idiomatic and deterministic code for all platforms, replacing handwritten boilerplate. So far, Connect-Swift has made it incredibly easy to build iOS APIs, and Connect-Web and Connect-Go have done the same for the frontend and backend.
Today we are releasing Connect-Kotlin which brings this experience to Kotlin and enables Android engineers to collaborate with their Swift, Go, and TypeScript counterparts to build mobile applications completely end-to-end using Protobuf, Connect, gRPC, and gRPC-Web. With Connect-Kotlin, the family of Connect libraries now has full first class support on mobile!
If you want to go right to a hands-on demo, we created a getting started guide for building a Connect-enabled Kotlin chat app in ~10 minutes.
The following .proto
file defines an ElizaService
containing a Say
RPC (Remote Procedure Call, essentially an HTTP endpoint) that accepts a SayRequest
and returns a SayResponse
, each containing a sentence
string field:
package connectrpc.eliza.v1;
message SayRequest {
string sentence = 1;
}
message SayResponse {
string sentence = 1;
}
service ElizaService {
rpc Say(SayRequest) returns (SayResponse) {}
}
Connect-Kotlin’s protoc-gen-connect-kotlin
generator plugin then uses this file to generate a simple client interface (ElizaServiceClientInterface.kt
) and implementation class (ElizaServiceClient.kt
) to communicate with the defined ElizaService
:
// ElizaServiceClientInterface.kt
public interface ElizaServiceClientInterface {
public suspend fun say(request: SayRequest, headers: Headers = emptyMap()):
ResponseMessage<SayResponse>
}
// 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",
connectrpc.eliza.v1.SayRequest::class,
connectrpc.eliza.v1.SayResponse::class,
),
)
}
Code hints are provided within the IDE to indicate how to use these generated classes for making requests, and they’re typically consumed within an Android Activity:
fun suspend talkToEliza(sentence: String) {
// Make a request to Eliza.
val sayRequest = SayRequest.newBuilder()
.setSentence(sentence)
.build()
val response = elizaServiceClient.say(sayRequest)
val elizaSentence = response.success { success ->
// Get Eliza's reply from the response.
success.message.sentence
}
view.setText(elizaSentence)
}
Although the SayRequest
and SayResponse
models above reference outputs from Google’s Java Protobuf generator plugin (protoc-gen-java
), Connect-Kotlin supports several Protobuf model generators, including:
protoc-gen-java
), along with the javalite
option.protoc-gen-kotlin
), along with the kotlinlite
option.square/wire
generator.Connect-Kotlin supports three protocols out of the box:
We’re excited for you to try out Connect-Kotlin! Here are some resources to help you dive in:
Connect-Kotlin is in beta, so we’re all ears for feedback - you can reach us through the Buf Slack or by filing a GitHub issue and we’d be more than happy to chat!