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.
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.
Add the Gradle dependency
Add the SDK to your module-level build.gradle.kts.
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.
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.
Clean up on lifecycle
Disconnect when the Activity or ViewModel is destroyed.
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.
Related Docs
Read the core WebSocket guides
Related Use Cases
Explore realtime use cases
Start building realtime Kotlin apps
Completely free. No credit card. No catch.