Swift WebSocket Libraries: Starscream and URLSessionWebSocketTask

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

WebSocket Libraries for Swift (iOS/macOS): Starscream and URLSessionWebSocketTask



WebSocket is a protocol that provides a persistent two-way connection between a client (iOS/macOS application) and a server. Unlike HTTP, after establishing a connection, data can be transmitted in real-time without repeated requests. For Swift, there are two main approaches: using the built-in URLSessionWebSocketTask (available since iOS 13/macOS 10.15) and the third-party library Starscream (supports older versions and provides extended functionality).



1. URLSessionWebSocketTask — Native WebSocket in Swift



Starting with iOS 13 and macOS 10.15, Apple added WebSocket support directly into URLSession. This means that for basic scenarios, no additional dependencies are required.



Example of connecting and sending a message



import Foundation

class WebSocketManager: NSObject { private var webSocketTask: URLSessionWebSocketTask? private let session = URLSession(configuration: .default) func connect() { guard let url = URL(string: "wss://echo.websocket.org") else { return } webSocketTask = session.webSocketTask(with: url) webSocketTask?.resume() receiveMessage() } func sendMessage(_ text: String) { let message = URLSessionWebSocketTask.Message.string(text) webSocketTask?.send(message) { error in if let error = error { print("Send error: \\(error.localizedDescription)") } } } private func receiveMessage() { webSocketTask?.receive { [weak self] result in switch result { case .success(let message): switch message { case .string(let text): print("Received: \\(text)") case .data(let data): print("Received binary data: \\(data.count) bytes") @unknown default: break } self?.receiveMessage() // continue listening case .failure(let error): print("Receive error: \\(error.localizedDescription)") } } } func disconnect() { webSocketTask?.cancel(with: .normalClosure, reason: nil) }}


Advantages of URLSessionWebSocketTask

  • No dependencies — everything is already in the system.
  • Native integration with background tasks and App Transport Security.
  • Support from Apple and updates along with the OS.


Disadvantages

  • Minimum version iOS 13 / macOS 10.15.
  • No built-in support for reconnection and ping/pong (needs to be implemented manually).
  • Less flexibility in configuration.


2. Starscream — Powerful and Flexible WebSocket Library for Swift



Starscream is the most popular third-party WebSocket library for Swift. It supports iOS 8+, macOS 10.10+, as well as watchOS and tvOS. Starscream provides a built-in reconnection mechanism, automatic ping/pong, and SSL/TLS support.



Installation via Swift Package Manager

// In Package.swift or via Xcode: File → Add Packages...// URL: https://github.com/daltoniam/Starscream.git// Version: 4.0.0 and above


Example of using Starscream



import Starscream

class StarscreamManager: WebSocketDelegate { var socket: WebSocket? func setupConnection() { var request = URLRequest(url: URL(string: "wss://echo.websocket.org")!) request.timeoutInterval = 5 socket = WebSocket(request: request) socket?.delegate = self socket?.connect() }\

Recommendations