DEV Community

Cover image for Swift WebSockets: Starscream or URLSession in 2021?
Matheus Cardoso
Matheus Cardoso

Posted on • Originally published at getstream.io

Swift WebSockets: Starscream or URLSession in 2021?

Building applications such as online games and real-time chat has never been easier since the standardisation of the WebSocket protocol in 2011. Before that, most app experiences were plagued with manual refreshes in order to access the latest data available (remember F5?). Since then, most apps use WebSockets in some form to update their user interface with new data as soon as it is generated.

For Swift programmers targeting iOS, macOS, and other Apple platforms in 2021, there are two main libraries that can be used to connect to a WebSocket server: Starscream and URLSession. While both are capable libraries, they also have advantages and disadvantages which are important to consider before choosing one over the other.

In this article, we'll go over several positive and negative characteristics of Starscream and URLSession to help you decide which fits best with your requirements. Let's dig in!

What is the WebSocket Protocol

The WebSocket Protocol enables two-way communication between a client running untrusted code in a controlled environment to a remote host that has opted-in to communications from that code. The security model used for this is the origin-based security model commonly used by web browsers. The protocol consists of an opening handshake followed by basic message framing, layered over TCP. The goal of this technology is to provide a mechanism for browser-based applications that need two-way communication with servers that does not rely on opening multiple HTTP connections (e.g., using XMLHttpRequest or s and long polling).


Starscream

Image shows starscream github repo's banner image
Image from https://github.com/daltoniam/Starscream

Starscream is a conforming WebSocket (RFC 6455) library written in Swift targeting the iOS and macOS systems. Years before Apple's URLSession even supported WebSockets, Starscream was the Swift library of choice. For reasons we'll go over shortly, it is the library we use at Stream for building our iOS Chat SDK.

Pros

Legacy support

Starscream will compile for iOS 8+ (7+ with some tweaks) and macOS 10.10+. If supporting devices with old operating systems is a requirement, you have almost no choice but to go with this library. This is the one of the main reasons we chose to go with Starscream for the Stream Chat iOS SDK which supports iOS 11 onwards.

Battle tested

Starscream has been around for a long time. It's been tested by thousands of apps and improved upon for years. It presents a reliability that may be currently unmatched among WebSocket libraries. This is another important point in our decision to stick with Starscream and it will be obvious (spoiler alert) when we talk about URLSession's reliability issues for WebSockets.

Open-Source

Starscream is fully open-source up to its usage of CFNetwork calls, so it's possible to make changes if you need. It's also possible to use URLSession underneath for supported systems, but that's not enabled by default.

PS: Technically, CFNetwork and URLSession are open-source, but you're not meant to compile your own version of them for production use in Apple platforms so it doesn't count, right?

Cons

Binary Size

Since Starscream is not shipped with Apple's operating systems, including it in your project will cause an increase in your binary size of about 500kb which is not terrible for regular apps, but could be a real issue for size-restrictive targets such as the new App Clips.

Maintenance

Since the release of URLSession support for WebSockets in 2019, the Starscream repository has seen a steady decrease in activity with many unanswered issues and pull requests, with the last commit merged in August 2020. This is a sign that if you run into bugs or need a new feature it could be a dead end unless you handle it yourself.


URLSession

Image shows apple logo

URLSession is Apple's own high-level networking library for HTTP(S) requests. To be more precise, URLSession is really a class that's part of the Foundation library. It coordinates a group of related, network data-transfer tasks, and, as of iOS 13 and macOS 10.15, it supports WebSockets as one of those tasks (URLSessionWebSocketTask).

Pros

Binary size

Using URLSession in your app will not cause any noticeable increase in your final binary size since it's included in the system's Foundation library. When you need to optimize for size, such as when building an App Clip, then URLSession is your library of choice!

Maintained By Apple

URLSession is maintained by Apple and should receive regular updates for the foreseeable future, though they can only be taken advantage by installing new operating system patches. You can also ask for support directly from Apple if you are enrolled in their paid developer program.

Cons

No Legacy Support

URLSession only received WebSockets support in 2019 when iOS 13 and macOS 10.15 were released. As such, using it for Web Sockets will restrict your app to those versions and up.

Closed-Source

URLSession is closed-source, so it's not possible to make any changes to the underlying implementation in case you need them. Again, using a looser definition of close-source, since technically you can get the source code on GitHub, but it's not viable to compile your own version of it.

Unreliability & Crashes

It's expected that a function developed not long ago still presents rough edges. If you search the web for "URLSessionWebSocketTask crash" or "URLSessionWebSocketTask bug", you'll find many instances on StackOverflow and even Apple's forums of developers reporting crashes and other issues.


Which You Should Choose

Given URLSession has implemented support for WebSockets relatively recently, it's not currently as reliable as Starscream which existed for years prior. Many projects that migrated from Starscream to URLSession reported mysterious bugs and crashes. This alone gives Starscream the top spot among Swift WebSocket libraries. But keep on reading, there are other things to consider.

When Apple fixes the reliability issues, the most defining point in choosing these libraries is going to be about legacy support. If your app needs to support system versions lower than iOS 13 / macOS 10.15, your best bet will be to use Starscream. If your app only supports those versions and up, it will be better to take advantage of the native URLSession framework and Apple's extended support of it.

For App Clips, URLSession is currently preferred due to Starscream's 500kb binary size impact. With just a little bit of luck, you might escape its reliability issues.

Top comments (0)