avatarKristaps Grinbergs

Summary

The provided content discusses the implementation and usage of WebSockets in iOS using Swift, with a focus on the WebSocket protocol, its lifecycle, and the simplification of this process through the use of the Starscream library.

Abstract

WebSockets enable fast, secure, and bidirectional communication over a single TCP connection, which is particularly useful for real-time applications such as chat services and live data feeds. The article outlines the WebSocket protocol, including the initial handshake process, data exchange format, and connection termination procedures. It also addresses the complexities of implementing WebSockets in iOS, macOS, tvOS, or watchOS, and introduces the Starscream library as a solution to streamline the creation and management of WebSocket connections in Swift. The library handles intricate tasks like connection upgrades and frame formatting, allowing developers to focus on sending and receiving data through simple API calls.

Opinions

  • The author suggests that implementing WebSockets in iOS and related Apple operating systems is non-trivial, implying it can be challenging without proper tools or libraries.
  • The use of the Starscream library is recommended as it abstracts away the complexities of WebSocket communication, making it more accessible for developers working with Swift.
  • The article conveys that WebSockets are a superior choice for scenarios requiring real-time data exchange due to their speed and efficiency compared to traditional HTTP polling.
  • Security is highlighted as an important aspect of WebSockets, with the inclusion of the Sec-WebSocket-Key during the handshake process to prevent unauthorized cross-origin access.
  • The author emphasizes the importance of understanding the WebSocket protocol, including the specific control sequences for closing connections and the exchange of ping and pong messages to maintain connectivity.

WebSockets in iOS Using Swift

WebSockets allow for extremely fast two-way networking communication

Photo by Christopher Gower on Unsplash

WebSockets allow for extremely fast two-way networking communication, which lets you send and receive updates quicker and more often, not to mention securely.

WebSocket is a communication protocol that uses sockets, providing duplex communication over a single TCP connection. It was standardized by the IETF in 2011 and is a different protocol from HTTP.

WebSockets are widely used in chat apps, streaming real-time notifications, and stock prices.

WebSocket Lifecycle

Shaking hands with the server

The handshake app is the web part in WebSockets. It’s the bridge from HTTP to WebSocket. The client sends a pretty standard HTTP GET request to the server:

GET /chat HTTP/1.1
Host: example.com:8000
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 1

The most interesting thing here is Sec-WebSocket-Key, which is needed for security reasons and is generated according to the WebSocket standard.

The server validates the request and, if everything is fine, sends back an HTTP response:

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=

Exchange the information

At any time, the server or client can send data that follows this specific format:

WebSocket frame format

I will not go over each part, but you can find out more in the standard.

Pings and pongs

At any point, the client or server can send a ping and the other party must send back a pong.

Closing

The connection can be closed by any party by sending a specified control sequence.

Implementing WebSockets in iOS

Implementing WebSockets in iOS, macOS, tvOS or watchOS isn’t a trivial task. New Network.framework can simplify that but you still need to deal with underlying tasks like upgrading connection and setting up a WebSocket frame.

Starscream

The Swift WebSocket client library Startscream simplifies all the heavy-lifting tasks. Install the library and import it in any Swift file.

import Starscream

Creating the connection

After that, create a connection and set up the delegate.

let url = URl(string: "ws://echo.websocket.org")!
  let request = URLRequest(url: url)
  let websocket = WebSocket(request: request)

Setting up delegation

Then we need to set up delegate and implement didReceive method with event WebSocketEvent type.

func didReceive(event: WebSocketEvent, client: WebSocket) {
    switch event {
    case .connected(let headers):
      print("connected \(headers)")
    case .disconnected(let reason, let closeCode):
      print("disconnected \(reason) \(closeCode)")
    case .text(let text):
      print("received text: \(text)")
    case .binary(let data):
      print("received data: \(data)")
    case .pong(let pongData):
      print("received pong: \(pongData)")
    case .ping(let pingData):
      print("received ping: \(pingData)")
    case .error(let error):
      print("error \(error)")
    case .viabilityChanged:
      print("viabilityChanged")
    case .reconnectSuggested:
      print("reconnectSuggested")
    case .cancelled:
      print("cancelled")
    }
  }

Once this is done, we can start the connection. Making the handshake and upgrading connection is done behind the scenes by the library.

socket.connect();

Sending data

There are several ways to send data:

  • binary
  • string
  • ping
  • pong

The easiest way is to just send a string:

socket.write(string: "Hi Server!")

Closing the connection

At any point, we can check close it if it’s not needed anymore.

websocket.disconnect(closeCode: CloseCode.goingAway.rawValue)

TL;DR

WebSocket isn’t a first-class citizen in iOS, macOS, tvOS and watchOS. It can be quite overwhelming to implement, use and understand it, but the Swift library Starscream can help you take care of all these tasks.

Links

Swift
iOS
iOS App Development
Websocket
Programming
Recommended from ReadMedium