Swift WebSockets: Starscream or URLSession in 2021?

Matheus Cardoso
Mac O’Clock
Published in
5 min readMar 3, 2021

--

Photo by Adem AY on Unsplash

Building applications such as online games and real-time chat has never been more straightforward since the standardization of the WebSocket protocol in 2011. Before that, most app experiences were plagued with manual refreshes 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 available.

For Swift programmers targeting iOS, macOS, and other Apple platforms in 2021, two main libraries help you connect to a WebSocket server: Starscream and Apple’s URLSession. While both are capable libraries, they also have advantages and disadvantages that are important to consider before choosing one.

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

--

--