gitmyhub

Starscream

Swift ★ 8.6k updated 2y ago

Websockets in swift for iOS and OSX

A Swift library for iOS and macOS that handles WebSocket connections with a few lines of code, letting your app send and receive live messages from a server without managing the protocol yourself.

SwiftiOSmacOSXcodesetup: easycomplexity 2/5

Starscream is a Swift library for iOS and macOS that adds WebSocket support to native Apple platform apps. WebSocket is a communication standard that keeps an open, two-way connection between a client and a server, so each side can send messages to the other at any time without the client having to repeatedly ask for updates. This is useful for things like live chat, real-time notifications, collaborative editing, or any feature where data needs to arrive as soon as it is available.

To use Starscream, you add it to your Xcode project through one of the standard Swift dependency tools, import the module, and then create a WebSocket object pointed at the server address you want to connect to. You register a delegate or a closure callback to handle events: when the connection opens, when a message arrives, when the connection drops, and when an error occurs. Sending a message back to the server is a single method call.

The library handles the underlying protocol details, including encrypted connections over WSS, automatic responses to server ping messages to keep the connection alive, and optional compression of the data being sent. You can also set custom HTTP headers on the initial connection request, which is sometimes required by servers that need authentication tokens or specific protocol identifiers during the handshake.

Installation is supported through Swift Package Manager, CocoaPods, and Carthage, which are all common ways to add third-party code to Apple platform projects. The README includes ready-to-paste configuration snippets for each method. The library targets iOS 8 or later and macOS 10.10 or later. It is aimed at iOS and macOS developers writing Swift apps who need to add live data connections to a server without building the WebSocket protocol handling themselves.

Where it fits