Skip to content

Repository files navigation

ApinatorSDK

Swift License: MIT CI

Swift client SDK for Apinator — real-time WebSocket messaging for iOS and macOS applications.

Features

  • Public, private, and presence channels
  • Automatic reconnection with exponential backoff
  • Client events on private/presence channels
  • Presence member tracking
  • Zero dependencies — uses only Foundation and URLSessionWebSocketTask
  • iOS 13+ / macOS 10.15+

Installation

Swift Package Manager

Add to your Package.swift:

dependencies: [
    .package(url: "https://github.com/apinator-io/sdk-swift.git", from: "1.0.0")
]

Or in Xcode: File > Add Package Dependencies and enter the repository URL.

Quick Start

import ApinatorSDK

let client = Apinator(appKey: "your-app-key", cluster: "eu")
client.connect()

let channel = client.subscribe("my-channel")

channel.bind("my-event") { data in
    print("Received: \(data)")
}

Channel Types

Public Channels

No authentication required. Any client can subscribe.

let channel = client.subscribe("news")
channel.bind("update") { data in /* ... */ }

Private Channels

Require server-side authentication. Prefix with private-.

let client = Apinator(options: RealtimeOptions(
    appKey: "your-app-key",
    cluster: "eu",
    authEndpoint: "https://your-server.com/api/realtime/auth"
))

let channel = client.subscribe("private-orders")
channel.bind("new-order") { data in /* ... */ }

Presence Channels

Like private channels, but also track who is subscribed. Prefix with presence-.

let presence = client.subscribe("presence-chat") as! PresenceChannel

presence.bind("realtime:subscription_succeeded") { _ in
    print("Members: \(presence.getMembers())")
    print("Me: \(String(describing: presence.me))")
}

presence.bind("realtime:member_added") { data in
    print("Joined: \(data)")
}

presence.bind("realtime:member_removed") { data in
    print("Left: \(data)")
}

Client Events

Trigger events directly from the client on private or presence channels:

let privateChannel = client.subscribe("private-chat")
try privateChannel.trigger("client-typing", data: ["user": "alice"])

Connection States

Monitor the connection lifecycle:

client.bind("state_change") { data in
    if let stateChange = data as? [String: String] {
        print("\(stateChange["previous"]!) -> \(stateChange["current"]!)")
    }
}

States: initialized -> connecting -> connected -> disconnected / unavailable

API Reference

See docs/api-reference.md for the full API.

Platform Support

  • iOS 13+
  • macOS 10.15+

Links

License

MIT — see LICENSE.

About

No description, website, or topics provided.

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages