Pluskode Client SDK for iOS - Swift client with HTTP, WebSocket, SSE, gRPC, MQTT, and Binary Stream support
npm install @pluskode/client-iosPackage.swift:
swift
dependencies: [
.package(url: "https://github.com/pluskode/client-ios.git", from: "0.1.0")
]
`
Or in Xcode:
1. File ā Add Packages...
2. Enter package URL: https://github.com/pluskode/client-ios.git
3. Select version
$3
`ruby
pod 'PluskodeClient', '~> 0.1.0'
`
Usage
$3
`swift
import PluskodeClient
let client = PluskodeClient(
options: PluskodeClientOptions(
baseURL: "http://localhost:3000",
timeout: 30.0,
retries: 3
)
)
// Set authentication
client.setAuth(type: "Bearer", token: "your-jwt-token")
`
$3
`swift
// GET request
let users: [User] = try await client.get("/api/users")
// POST request
let newUser = try await client.post(
"/api/users",
data: [
"name": "John",
"email": "john@example.com"
]
)
// PUT, PATCH, DELETE
try await client.put("/api/users/123", data: ["name": "Jane"])
try await client.patch("/api/users/123", data: ["email": "jane@example.com"])
try await client.delete("/api/users/123")
`
$3
`swift
// Subscribe to channel
let unsubscribe = client.subscribe(channel: "chat/room1") { data in
print("Message: \(data)")
}
// Send message
try client.send(channel: "chat/room1", data: ["text": "Hello!"])
// Unsubscribe
unsubscribe()
`
$3
`swift
let unsubscribe = client.subscribeSSE(path: "/events/stream") { event in
print("Event: \(event.event ?? "message"), Data: \(event.data)")
}
// Close
unsubscribe()
`
$3
`swift
let user = try await client.rpc(
service: "UserService",
method: "GetUser",
request: ["id": "123"]
) as User
`
$3
`swift
// Subscribe
let unsubscribe = client.subscribeMQTT(
topic: "sensors/temperature",
callback: { topic, message, qos in
print("\(topic): \(message) (QoS: \(qos))")
},
options: MQTTOptions(qos: 1)
)
// Publish
try await client.publishMQTT(
topic: "sensors/temperature",
message: "25.5",
options: MQTTOptions(qos: 1, retain: false)
)
`
$3
`swift
let close = client.connectBinary(path: "/custom-protocol") { data in
print("Received \(data.count) bytes")
}
// Close
close()
`
$3
`swift
import SwiftUI
import PluskodeClient
struct UsersView: View {
@State private var users: [User] = []
@State private var isLoading = false
let client = PluskodeClient(
options: PluskodeClientOptions(baseURL: "http://localhost:3000")
)
var body: some View {
List(users) { user in
Text(user.name)
}
.task {
isLoading = true
do {
users = try await client.get("/api/users")
} catch {
print("Error: \(error)")
}
isLoading = false
}
}
}
`
Project Structure
`
client/ios/
āāā Sources/
ā āāā PluskodeClient/
ā āāā PluskodeClient.swift
ā āāā EventSource.swift
āāā Tests/
āāā Package.swift
āāā README.md
``