Starscream — WebSocket library for Swift (iOS/macOS)
Starscream is a lightweight but powerful library for working with the WebSocket protocol on Apple platforms. Written entirely in Swift, it has no external dependencies and supports iOS, macOS, tvOS, and watchOS. If you need to organize real-time two-way communication between a mobile app and a server (chat, notifications, online games) — Starscream is an excellent choice.
Key Features
- Full support for RFC 6455 standard (WebSocket Protocol).
- Works over secure
wss://and regularws://connections. - Automatic reconnection on connection loss.
- Frame support: text, binary data (Data), ping/pong.
- Easy integration via CocoaPods, Swift Package Manager, or Carthage.
- Compatibility with Combine (iOS 13+) and Swift Concurrency (async/await).
Installation via Swift Package Manager (recommended)
Add the package in Xcode: File → Add Packages and paste the repository URL:
https://github.com/daltoniam/Starscream.gitSelect the version (latest stable) and add it to your target.
Basic Connection Example
Let's create a simple WebSocket client that connects to a server and displays received messages.
import Starscream
class WebSocketManager { var socket: WebSocket? func connect() { var request = URLRequest(url: URL(string: "wss://echo.websocket.org")!) request.timeoutInterval = 5 socket = WebSocket(request: request) socket?.delegate = self socket?.connect() }}
extension WebSocketManager: WebSocketDelegate { func didReceive(event: WebSocketEvent, client: WebSocket) { switch event { case .connected(let headers): print("Connected! Headers: \\(headers)") case .disconnected(let reason, let code): print("Disconnected: \\(reason) (code: \\(code))") case .text(let string): print("Received text: \\(string)") case .binary(let data): print("Received binary data: \\(data.count) bytes") case .ping(let data): print("Ping: \\(data?.hexString ?? "no data")") case .pong(let data): print("Pong: \\(data?.hexString ?? "no data")") case .viabilityChanged(let isViable): print("Connection viable: \\(isViable)") case .reconnectSuggested(let shouldReconnect): print("Reconnect suggested: \\(shouldReconnect)") case .cancelled: print("Connection cancelled") case .error(let error): print("Error: \\(error?.localizedDescription ?? "unknown error")") } }}Sending Data
Starscream allows sending both text strings and binary data. Example of sending JSON:
func sendMessage(text: String) { let message: [String: Any] = [ "type": "chat", "content": text, "timestamp": Int(Date().timeIntervalSince1970) ] if let jsonData = try? JSONSerialization.data(withJSONObject: message, options: []), let jsonString = String(data: jsonData, encoding: .utf8) { socket?.write(string: jsonString) }}
// Sending binary file (e.g., image)func sendImage(data: Data) { socket?.write(data: data)}Working with Combine (iOS 13+)
For reactive programming, Starscream provides a Publisher:
import Combine
class CombineViewModel: ObservableObject { @Published var lastMessage: String = "" private var socket: WebSocket? private var cancellables = Set<AnyCancellable>() func setup() { var req Also in library
Elasticsearch Client for Java: Complete Guide to Connection and Work
GraphQL Library in C++: Implementing API with graphql-cpp
Internationalization (i18n) in TypeScript: A Complete Guide
OpenAI SDK for JavaScript (Node.js): Complete Guide with Examples
Markdig for C# (.NET): Complete Guide with Examples