Using Cloud Pub/Sub from Kotlin

Megan Potter
Google Cloud - Community
3 min readMar 30, 2021

--

Kotlin logo
Cloud Pub/Sub logo

Why Kotlin on the server side?

In regards to Google products, Kotlin seems most well known as a language to build Android applications. But Kotlin’s lovely type safety and code brevity features work great on the server side, as well. And since it compiles and runs natively on the JVM, it is pretty easy to slot it into any place where you’d run a Java application.

Thankfully, it is also quite easy to use the existing Pub/Sub libraries written for Java.

(Please note that this is not officially supported at this time, but hopefully this will get you going if you’re trying it!)

Project setup

It’s possible to create Kotlin projects that build using both Maven and Gradle, but Gradle is the preferred and “native” choice for Kotlin, so I will proceed with that.

The first step is to use either IntelliJ IDEA or Android Studio to create a Kotlin command line project that builds with Gradle. Make sure to make a Console Application, and to set a JDK. (It doesn’t have to be 11.)

There’s not much to change on the second page:

To work with the Pub/Sub libraries, we must add them to our Gradle build configuration. Edit the build.gradle.kts file, and add the following implementation line:

Of course, that is the current version as of this article; you should probably visit the Maven Central entry for the Pub/Sub library and check for the latest version.

(If you have Gradle build errors, be aware that a recent IntelliJ update caused some of those; make sure you’re on 2020.3.1+.)

You should now be able to make calls to the Java Pub/Sub library, like this one:

Compilation should succeed, though it won’t do much just yet!

Interfacing with Pub/Sub

Now that your basic project is in place, you can use the Java APIs but with the brevity and type safety in Kotlin that we know and love. For example, this function will begin listening on a subscription, calling the message receiver for each message received, until an exception happens.

You could use that function in something like this sample, which will simply receive and ack messages, and occasionally (once per 10 seconds) print a count of how many have been received:

Next steps

From here, you can start to do things like creating fluent data classes for your message schema. The examples I gave above are also pretty simplistic, and maybe not quite idiomatic (apologies for being a bit rusty!) They could probably be improved using Kotlin flow operators and such. Kotlin continuations might also be used very gracefully to handle async communication.

See a complete example which runs on a local emulator.

Have you used Kotlin on the server side? Please feel free to leave a comment about this post or things that you find interesting/promising in regards to Kotlin on Google Cloud Platform!

--

--

Megan Potter
Google Cloud - Community

Software Engineer at Google, for Google Cloud Platform, in Ontario, Canada