Real-world examples showing how to use the OddSockets Swift SDK in production applications
View on GitHubimport OddSockets
// Create configuration
let config = OddSocketsConfig.Builder()
.apiKey("ak_live_1234567890abcdef")
.build()
// Initialize client
let client = OddSockets(config: config)
// Get channel
let channel = client.channel("my-channel")
// Subscribe to messages
try await channel.subscribe { message in
print("Received: \(message.data)")
}
// Publish a message
try await channel.publish("Hello, World!")
import SwiftUI
import OddSockets
struct ChatView: View {
@StateObject private var channel: OddSocketsChannel
@State private var messageText = ""
init() {
let config = OddSocketsConfig.Builder()
.apiKey("ak_live_1234567890abcdef")
.build()
let client = OddSockets(config: config)
_channel = StateObject(wrappedValue: client.channel("chat"))
}
var body: some View {
VStack {
List(channel.messageHistory, id: \.id) { message in
Text(message.data?.description ?? "")
}
HStack {
TextField("Message", text: $messageText)
Button("Send") {
Task {
try await channel.publish(messageText)
messageText = ""
}
}
}
}
.task {
try? await channel.subscribe { message in
// Messages automatically update via @Published
}
}
}
}
import UIKit
import OddSockets
import Combine
class ChatViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var messageTextField: UITextField!
@IBOutlet weak var sendButton: UIButton!
private var messages: [Message] = []
private var cancellables = Set()
private let client: OddSockets
private let channel: OddSocketsChannel
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
let config = OddSocketsConfig.Builder()
.apiKey("ak_live_1234567890abcdef")
.build()
client = OddSockets(config: config)
channel = client.channel("uikit-chat")
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
connectToChannel()
}
private func connectToChannel() {
// Subscribe to messages using Combine
channel.messagePublisher
.receive(on: DispatchQueue.main)
.sink { [weak self] message in
self?.messages.append(message)
self?.tableView.reloadData()
}
.store(in: &cancellables)
}
}
import Combine
import OddSockets
class ChatService: ObservableObject {
@Published var messages: [Message] = []
@Published var connectionStatus: ConnectionStatus = .disconnected
@Published var presenceInfo: PresenceInfo?
private let client: OddSockets
private let channel: OddSocketsChannel
private var cancellables = Set()
enum ConnectionStatus {
case disconnected, connecting, connected, error(String)
}
init(apiKey: String, channelName: String) {
let config = OddSocketsConfig.Builder()
.apiKey(apiKey)
.build()
client = OddSockets(config: config)
channel = client.channel(channelName)
setupPublishers()
}
private func setupPublishers() {
// Message publisher
channel.messagePublisher
.receive(on: DispatchQueue.main)
.sink { [weak self] message in
self?.messages.append(message)
}
.store(in: &cancellables)
// Presence publisher
channel.presencePublisher
.receive(on: DispatchQueue.main)
.assign(to: \.presenceInfo, on: self)
.store(in: &cancellables)
}
}
import SwiftUI
import WatchKit
import OddSockets
struct WatchChatView: View {
@StateObject private var viewModel = WatchChatViewModel()
@State private var isShowingMessageInput = false
var body: some View {
NavigationView {
VStack {
if viewModel.messages.isEmpty {
Text("No messages yet")
.foregroundColor(.secondary)
} else {
List(viewModel.messages.suffix(10), id: \.id) { message in
VStack(alignment: .leading, spacing: 2) {
Text(message.data?.description ?? "")
.font(.caption)
Text(message.timestamp, style: .time)
.font(.caption2)
.foregroundColor(.secondary)
}
}
}
Button("Send Message") {
isShowingMessageInput = true
}
.buttonStyle(.borderedProminent)
}
.navigationTitle("Chat")
}
.task {
await viewModel.connect()
}
}
}
import SwiftUI
import AppKit
import OddSockets
@main
struct MacChatApp: App {
@StateObject private var chatService = ChatService()
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(chatService)
.frame(minWidth: 600, minHeight: 400)
}
.commands {
CommandGroup(after: .newItem) {
Button("Connect to Channel") {
Task {
await chatService.connect()
}
}
.keyboardShortcut("c", modifiers: .command)
}
}
MenuBarExtra("OddSockets", systemImage: "message") {
VStack {
Text("Messages: \(chatService.messages.count)")
Button("Show Main Window") {
NSApp.activate(ignoringOtherApps: true)
}
}
.padding()
}
}
}
struct ContentView: View {
@EnvironmentObject var chatService: ChatService
@State private var messageText = ""
var body: some View {
HSplitView {
// Sidebar with channels
ChannelSidebar()
.frame(minWidth: 200)
// Main chat area
ChatArea(messageText: $messageText)
}
.toolbar {
ToolbarItem(placement: .primaryAction) {
Button("Send") {
Task {
await chatService.sendMessage(messageText)
messageText = ""
}
}
.disabled(messageText.isEmpty)
}
}
}
}