On this page

Kotlin API & SDK Docs 12.0.1

Breaking changes in v9.0.0

PubNub Kotlin SDK version 9.0.0 unifies the codebases for Kotlin and Java SDKs, introduces a new way of instantiating the PubNub client, and changes asynchronous API callbacks and emitted status events. These changes can impact applications built with previous versions (< 9.0.0 ) of the Kotlin SDK.

For more details about what has changed, refer to Java/Kotlin SDK migration guide.

This guide walks you through a simple "Hello, World" application that demonstrates the core concepts of PubNub:

  • Setting up a connection
  • Sending messages
  • Receiving messages in real-time

Overview

This guide will help you get up and running with PubNub in your Kotlin application. Since Kotlin is commonly used across different platforms, we provide two implementation paths:

  • Android app development: For developers building Android applications with Kotlin
  • Non-mobile platforms: For developers using Kotlin in other environments (server-side, desktop, etc.)

The core PubNub concepts and API usage remain the same across both paths, but implementation details like lifecycle management and UI updates differ. Select the appropriate tab in each section to see platform-specific guidance.

Chat applications

If you want to create a mobile chat application on Android with PubNub, refer to Kotlin Chat SDK for details on all available chat features.

Prerequisites

Before we dive in, make sure you have:

  • A basic understanding of Kotlin
  • Android Studio or your preferred Kotlin IDE
  • A PubNub account (we'll help you set this up!)

Setup

Get your PubNub keys

First, get your PubNub keys:

  • Sign in or create an account on the PubNub Admin Portal.
  • Create an app (or use an existing one).
  • Find your publish and subscribe keys in the app dashboard.

When you create an app, PubNub automatically generates a keyset. You can use the same keyset for development and production, but we recommend separate keysets for each environment to improve security and management.

Install the SDK

SDK version

Always use the latest SDK version to have access to the newest features and avoid security vulnerabilities, bugs, and performance issues.

To integrate PubNub into your Android project, add this dependency to your app-level build.gradle file:

1implementation 'com.pubnub:pubnub-kotlin:12.0.1'
2
3// Optional: Add Kotlin coroutines for better async handling
4implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4'

Don't forget to add internet permission to your AndroidManifest.xml:

1<uses-permission android:name="android.permission.INTERNET" />
Pro tip

You can also add the dependency through the Android Studio UI:

  • Right-click on your project.
  • Select Open Module Settings.
  • Go to the Dependencies tab.
  • Click the + button.
  • Search for pubnub-kotlin.

Configure ProGuard

If you're using ProGuard, configure it as follows.

1# Add project specific ProGuard rules here.
2# You can control the set of applied configuration files using the
3# proguardFiles setting in build.gradle.
4#
5# For more details, see
6# http://developer.android.com/guide/developing/tools/proguard.html
7
8# If your project uses WebView with JS, uncomment the following
9# and specify the fully qualified class name to the JavaScript interface
10# class:
11#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12# public *;
13#}
14
15# Uncomment this to preserve the line number information for
show all 65 lines

Steps

Initialize PubNub

In Android Studio, create a new Android project with Kotlin support. Add PubNub initialization code to your main Activity or Fragment class. This is the minimum configuration you need to send and receive messages with PubNub in your Android application.

Make sure to replace the demo keys with your app's publish and subscribe keys from the Admin Portal.

1// Import required classes
2import com.google.gson.JsonObject
3import com.pubnub.api.PubNub
4import com.pubnub.api.UserId
5import com.pubnub.api.v2.PNConfiguration
6import androidx.appcompat.app.AppCompatActivity
7import android.os.Bundle
8import kotlinx.coroutines.CoroutineScope
9import kotlinx.coroutines.Dispatchers
10import kotlinx.coroutines.launch
11
12class PubNubActivity : AppCompatActivity() {
13 private lateinit var pubnub: PubNub
14
15 override fun onCreate(savedInstanceState: Bundle?) {
show all 40 lines