Using the PubNub Kotlin Chat SDK

PubNub's Kotlin Chat SDK is the recommended way for Android developers to create new chat applications or add in-app chat to existing applications

Step 01

Chat SDK Tutorial: Environment Setup for Kotlin

This tutorial walks you through a basic Android sample app created with PubNub Kotlin Chat SDK 1.0.0. It explains how the sample is structured, how the app initializes chat, and how the messaging features map to the Kotlin Chat SDK APIs. The sample app is available at https://github.com/PubNubDevelopers/Chat-SDK-Tutorial-Kotlin and is released under MIT. The sample is designed for learning and has minimal error handling.


Note: This written tutorial and the repository have been refreshed for PubNub Kotlin Chat SDK 1.0.0. Some video footage and screenshots may show an earlier version of the UI, but the code snippets below match the upgraded sample.


Prerequisites

The Kotlin Chat SDK is UI and framework-agnostic, so you control how your application looks and how it is architected. The sample application uses Jetpack Compose with a small custom UI.


Make sure you have the following installed on your computer:

Download the Source Application

In a terminal or command window, navigate to an empty folder. Enter the following command to clone the repository.

Code
git clone https://github.com/PubNubDevelopers/Chat-SDK-Tutorial-Kotlin.git

Install dependencies

Open the Chat-SDK-Tutorial-Kotlin folder in Android Studio. Gradle will download the required Android, Compose, and PubNub dependencies. If you experience build issues with a newer Android Studio version, contact the DevRel team at devrel@pubnub.com

Open the following file:

/app/src/main/java/com/pubnub/chatsdk/kotlintutorial/Keys.kt

The file contains placeholders for your PubNub publish and subscribe keys:

Code
package com.pubnub.chatsdk.kotlintutorial

//  REPLACE THESE WITH YOUR PUB / SUB KEYS
val PUBNUB_PUBLISH_KEY="PUBLISH_KEY"
val PUBNUB_SUBSCRIBE_KEY="SUBSCRIBE_KEY"

The next step is to create your own publish and subscribe keys to replace these placeholders.

Step 02

Create keys

Demo keys

You're ready to go!

We have assigned demo keys that are set up to support the features of this tutorial app. These keys will be active for 72 hours, but you can sign up for a free account at the end of the tutorial to create permanent keys for your own app.

The demo keys have the following functionality activated:

  • Presence This lets the app show whether users are online and powers presence-aware UI.
  • Message Persistence This stores message history so the app can load earlier messages and reactions when a user reopens a channel.
  • App Context This stores user, channel, and membership metadata used by the sample app.

My own keys

To use your own keys, create a free account and a keyset with Presence, Message Persistence, and App Context on.

You can create the keyset with the Admin API, PubNub's REST API for account management. An Account Owner or Account Admin creates a Service Integration and its API key in the Admin Portal under Organization Settings → API Management. Give it read & write access to apps and keysets, plus read access to secret keys, which keyset creation requires. With that key, create an app, then create a keyset in it and pass the keyset's configuration in the same request. The response includes the publish and subscribe keys. See the Admin API documentation for the requests and keyset settings.

Step 03

Build and run

Step 04

Congratulations - what's next?

Chat SDK Documentation

Understand the Source Code

This tutorial has only scratched the surface of the Kotlin Chat SDK. Check out the SDK source code, available at https://github.com/pubnub/kmp-chat

The source code for the sample app discussed in this tutorial is available from https://github.com/PubNubDevelopers/Chat-SDK-Tutorial-Kotlin

Chat SDK Demos in other Languages

Experience our fully-featured group chat app for the web, written in TypeScript and Next.js:

Tutorial - Chat SDK - React Chat Demo

Ready to get beyond Proof of Concept?

Schedule a 15 minute chat with one of our experts to get your project moving

Talk to an Expert

Got a question for our developer relations team? Email devrel@pubnub.com

Keep building

Take it to production on your own keys

Create a free PubNub account to get permanent keys for this app.