Installation
Follow the steps to set up and start using the Swift ADK in your project.
1. Add via Swift Package Manager
Using Xcode
- Open your project in Xcode
- Go to File → Add Packages
- Enter the repository URL: "https://github.com/aiotrixdev/art-swift-adk.git"
Using Package.swift
dependencies: [
.package(url: "https://github.com/aiotrixdev/art-swift-adk.git", from: "1.0.0")
]
2. Import the ADK into your project
Import the ADK in your Swift files to use the features provided by ART:
import ArtAdk
3. Configure Client Credentials
Once you have access to the ART Live Dashboard, generate the Client Secret and Create a credential object:
let creds = CredentialStore(
clientID: "xxxxxxxxxx",
clientSecret: "xxxxxxxxxxx",
orgTitle: "YOUR_ORG",
projectKey: "YOUR_PROJECT_KEY",
environment: "YOUR_ENV"
)
- Drag
adk-services.jsoninto Xcode - Ensure:
- "Add to target" is checked
The adk-services.json needs to be updated with ProjectKey and Environment, manually. To learn how to obtain the values, click here
4. Get Passcode
Before authenticating, obtain a passcode for the user. Pass the credentials loaded in step 3:
func fetchPasscode(creds: CredentialStore) async throws -> String {
let url = URL(string: "https://dev.arealtimetech.com/ws/v1/connect/passcode")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue(creds.clientID, forHTTPHeaderField: "Client-Id")
request.addValue(creds.clientSecret, forHTTPHeaderField: "Client-Secret")
request.addValue(creds.orgTitle, forHTTPHeaderField: "X-Org")
request.addValue(creds.environment, forHTTPHeaderField: "Environment")
request.addValue(creds.projectKey, forHTTPHeaderField: "ProjectKey")
let body: [String: Any] = [
"username": "your_username",
"first_name": "first_name",
"last_name": "last_name"
]
request.httpBody = try JSONSerialization.data(withJSONObject: body)
let (data, _) = try await URLSession.shared.data(for: request)
let json = try JSONSerialization.jsonObject(with: data) as? [String: Any]
let dataObj = json?["data"] as? [String: Any]
return dataObj?["passcode"] as? String ?? ""
}
5. Authenticate
Once the client credentials are loaded and you have a passcode, initialize the ADK and connect.
For short-lived workflows, wrap connect / disconnect in a try / finally:
let passcode = try await fetchPasscode(creds: creds)
let config = AdkConfig(
uri: "YOUR_WEBSOCKET_URI",
authToken: passcode,
getCredentials: { creds }
)
let adk = Adk(config: config)
do {
try await adk.connect()
// Your logic here
} finally {
await adk.disconnect()
}
6. Subscribe to Channel
A Channel is a communication pathway that provides passage for data transfer. Subscribing to a channel lets you use real-time messaging in your application:
// Subscribe to your regular channel
let subscription = try await adk.subscribe(channel: "your-channel-name")
// For subscribing secure channel need this extra steps to Generate a new key pair and register the public key with the ART server
try await adk.generateKeyPair()
final subscription = await adk.subscribe(channel: "your-secure-channel-name");
You can learn more about Channel Configuration from here
7. Push Messages
Messages are pushed to a channel using the push() method. This method allows you to send various event types with associated data, and optionally target specific users within the channel for granular delivery.
// Define the message payload
let payload: [String: Any] = [
"content": "Hello from ART ADK!"
]
let targetUsers = ["username1", "username2"]
do {
try await subscription.push(
event: "message",
data: payload,
options: PushConfig(to: targetUsers)
)
print("Message pushed successfully")
} catch {
print("Failed to push message:", error)
}
🔒 Secure / targeted channels require exactly one recipient.
8. Listen to All Events and Messages
Bind handlers to events on the subscription's emitter. This delivers every incoming payload for the bound event type:
subscription.emitter.on("message") { data in
print("Received:", data)
}
To listen to every event on a default (non-CRDT) channel, cast and use listen():
subscription.listen { data in
print("Event \(data["event"] ?? "") → \(data["content"] ?? "")")
}
