Client SDK • Soon

Realtime WebSocket Notifications for Android & Kotlin

Add real-time WebSocket notifications to Android apps with a native Kotlin SDK. Built on OkHttp WebSocket, Gradle-ready, lifecycle-aware, and compatible with Jetpack Compose StateFlow.

Gradle $ implementation("io.apinator:apinator-kotlin:1.0.0")

Adding real-time WebSocket notifications to an Android app is straightforward with Apinator's Kotlin SDK. Built on OkHttp — the industry-standard HTTP client for Android — it opens a persistent WebSocket connection and delivers events to your ViewModels or Compose state. Lifecycle-aware design connects on start and disconnects cleanly when the ViewModel is cleared.

Quick Start

How to add WebSocket notifications to an Android app with Kotlin

Add realtime features to your Kotlin app in a few steps.

01

Add the Gradle dependency

Add the SDK to your module-level build.gradle.kts.

// build.gradle.kts dependencies { implementation("io.apinator:apinator-kotlin:1.0.0") }
02

Create a client and open a WebSocket connection

Initialize with your app key and call connect() to open a persistent WebSocket connection. OkHttp manages the transport layer.

import com.apinator.sdk.RealtimeClient import com.apinator.sdk.RealtimeOptions val client = RealtimeClient(RealtimeOptions( appKey = "your-app-key", cluster = "us", authEndpoint = "https://your-api.com/realtime/auth" )) client.connect()
03

Subscribe to a WebSocket channel

Subscribe to a private channel and bind event handlers. Incoming WebSocket events arrive in real-time with type-safe Kotlin callbacks.

val channel = client.subscribe("private-orders") channel.bind("new-order") { data -> val order = JSONObject(data) Log.d("Apinator", "New order: ${order.getString("id")}") }
04

Clean up on lifecycle

Disconnect when the Activity or ViewModel is destroyed.

// In your ViewModel override fun onCleared() { super.onCleared() client.disconnect() }

Why Kotlin

Built for Kotlin developers

OkHttp WebSocket

Built on OkHttp, the industry-standard HTTP client for Android. Reliable, efficient, and well-tested.

Gradle Ready

Single-line Gradle dependency. Published to Maven Central for easy integration.

Lifecycle Aware

Works with ViewModels, LiveData, and Compose. Connect on start, disconnect on cleared.

Presence Channels

Track who is online in your Android app. Show user avatars, typing indicators, and active users.

How WebSocket notifications work in an Android app

Android apps can receive background notifications through Firebase Cloud Messaging (FCM), but FCM is designed for system-level push notifications — it is not well-suited for real-time in-app updates where latency matters. For features like live chat, live auction bids, presence indicators, or real-time dashboards, a persistent WebSocket connection delivers much lower latency and higher throughput than push notifications.

Apinator's Kotlin SDK manages the WebSocket connection using OkHttp, the same HTTP client used by Square, Retrofit, and the majority of the Android ecosystem. The connection is managed inside a ViewModel, which is the standard Android architecture component for surviving configuration changes like screen rotation. When the ViewModel is cleared (onCleared), the SDK disconnects the WebSocket cleanly.

For Jetpack Compose apps, incoming WebSocket events flow through a MutableStateFlow, which Compose observes automatically using collectAsState(). This gives you the same reactive data flow you'd use for any other state — no WebSocket-specific UI handling needed. Private WebSocket channels require a backend authentication endpoint, supported by any server-side language that Apinator provides an SDK for.

Full Example

Full Example: Realtime WebSocket Notifications in Jetpack Compose

Complete WebSocket example: a ViewModel that manages the persistent WebSocket connection with StateFlow and a Jetpack Compose screen that displays real-time notifications.

NotificationsViewModel.kt
package com.example.app import androidx.lifecycle.ViewModel import com.apinator.sdk.RealtimeClient import com.apinator.sdk.RealtimeOptions import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow import org.json.JSONObject class NotificationsViewModel : ViewModel() { private val _notifications = MutableStateFlow(emptyList<String>()) val notifications = _notifications.asStateFlow() private val client = RealtimeClient(RealtimeOptions( appKey = "your-app-key", cluster = "us", authEndpoint = "https://api.example.com/realtime/auth" )) init { val channel = client.connect().subscribe("private-alerts") channel.bind("new-alert") { data -> val msg = JSONObject(data).getString("message") _notifications.value = _notifications.value + msg } } override fun onCleared() { super.onCleared() client.disconnect() } }

Start building realtime Kotlin apps

Completely free. No credit card. No catch.