Realtime WebSocket Notifications for iOS & Swift
Add real-time WebSocket notifications to iOS and macOS apps with a native Swift SDK. Built on URLSessionWebSocketTask — no third-party WebSocket library needed. SPM-ready for iOS 13+ and macOS 10.15+.
.package(url: "https://github.com/apinator-io/sdk-swift.git", from: "1.0.0") SPM Adding real-time WebSocket notifications to an iOS app is simple with Apinator's native Swift SDK. Built on Apple's URLSessionWebSocketTask, it opens a persistent WebSocket connection to Apinator's servers and delivers events directly to your SwiftUI views or UIKit controllers. No third-party WebSocket frameworks needed — pure Swift with Swift Package Manager distribution.
Quick Start
How to add WebSocket notifications to an iOS app with Swift
Add realtime features to your Swift app in a few steps.
Add the Swift package
Add the ApinatorSDK package to your Xcode project via Swift Package Manager.
// Package.swift
.package(
url: "https://github.com/apinator-io/sdk-swift.git",
from: "1.0.0"
) See your first event (no backend)
No server code yet: subscribe to the public channel my-channel (public channels need no auth endpoint) with your app key and cluster (us or eu) from the dashboard, then click the Send test event button on your app's Overview page in the dashboard. The event prints in the Xcode console. Keep a strong reference to the client, for example as a property. Open the dashboard
import ApinatorSDK
let client = Apinator(options: RealtimeOptions(appKey: "YOUR_APP_KEY", cluster: "us"))
client.connect()
client.subscribe("my-channel").bind("my-event") { data in
print("Received:", data)
} Create a client and open a WebSocket connection
Initialize the client with your app key and call connect() to open a persistent WebSocket connection to Apinator.
import ApinatorSDK
let client = Apinator(options: RealtimeOptions(
appKey: "your-app-key",
cluster: "us",
authEndpoint: "https://your-api.com/realtime/auth"
))
client.connect() Subscribe to a WebSocket channel
Subscribe to a private channel and bind event handlers with the chainable API. Events arrive in real-time over the WebSocket connection.
let channel = client.subscribe("private-notifications")
channel.bind("new-alert") { data in
// data is already-parsed JSON
if let json = data as? [String: Any] {
print("Alert:", json)
}
} Use presence for live users
Presence channels track who is online. Get member lists and member events.
let presence = client.subscribe("presence-room") as! PresenceChannel
// When subscription succeeds
presence.bind("realtime:subscription_succeeded") { _ in
print("Members:", presence.memberCount)
} Why Swift
Built for Swift developers
Native URLSession
Built on URLSessionWebSocketTask. No third-party WebSocket libraries needed. Works on iOS 13+ and macOS 10.15+.
Swift Package Manager
Add as a dependency via SPM. No CocoaPods or Carthage required. Clean dependency management.
Chainable API
Fluent method chaining: client.connect().subscribe("ch").bind("event") { data in ... }.
Auto-Reconnect
Exponential backoff reconnection with configurable max attempts. Your app stays connected.
How WebSocket notifications work in an iOS app
iOS apps typically receive background notifications through Apple Push Notification service (APNs), which requires a paid developer account, device registration, and a push notification provider. APNs works well for notifications delivered while the app is in the background, but it is not designed for real-time in-app updates — delivery latency can be several seconds, and there is no ordering guarantee.
For in-app real-time features — live feeds, chat messages, presence indicators, live auction bids — a persistent WebSocket connection is more appropriate. Apinator's Swift SDK opens and maintains a WebSocket connection using Apple's native URLSessionWebSocketTask and receives events in real-time as they are published from your backend. Unlike push notifications, this requires no APNs certificates, no device token registration, and no special app entitlements.
The SDK integrates naturally with SwiftUI through the ObservableObject pattern: bind incoming WebSocket events to @Published properties, and your views update automatically via Combine. It also handles auto-reconnect with exponential backoff, which is important on mobile where network changes (switching from WiFi to cellular) are common and should not require any code in your view layer.
Full Example
Full Example: Realtime WebSocket Notifications in SwiftUI
Complete WebSocket example: an ObservableObject that manages the persistent WebSocket connection and a SwiftUI view that displays live notifications via @Published.
import Foundation
import ApinatorSDK
class RealtimeManager: ObservableObject {
static let shared = RealtimeManager()
private let client: Apinator
@Published var notifications: [String] = []
private init() {
client = Apinator(options: RealtimeOptions(
appKey: "your-app-key",
cluster: "us",
authEndpoint: "https://api.example.com/realtime/auth"
))
}
func start() {
let ch = client.connect().subscribe("private-alerts")
ch.bind("new-alert") { [weak self] data in
guard let alert = data as? [String: Any],
let message = alert["message"] as? String else { return }
DispatchQueue.main.async {
self?.notifications.append(message)
}
}
}
func stop() {
client.unsubscribe("private-alerts")
client.disconnect()
}
}Related Docs
Read the core WebSocket guides
Related Use Cases
Explore realtime use cases
From the blog
Presence Channels Explained
Presence channels let you track who's online in real time — showing typing indicators, viewer counts, and live cursors without building your own member tracking.
What Is a WebSocket?
WebSockets let servers push data to clients the moment it changes — no polling, no waiting. Here's how they work, what they're good for, and when to use them.
WebSockets vs HTTP Polling
Polling gets the job done until it doesn't. Here's an honest comparison of WebSockets and HTTP polling — latency, server load, complexity, and when to use each.
Start building realtime Swift apps
Completely free. No credit card. No catch.