Advanced Networking with URLSession

Sep 15 2022 · Swift 5.6, iOS 15, Xcode 13.4.1

Part 1: Upload Data, Background Downloads & WebSockets

05. Understand Sockets

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 04. Background Downloads Next episode: 06. Connect to a WebSocket

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Transcript: 05. Understand Sockets

Networking can be painfully hard. It’s one of those systems that you don’t notice when it works well, but you definitely notices when something is going wrong.

Play any online game with spotty networking and you’ll understand the frustration.

While there are lots of definite network mechanisms, BSD sockets has been a defacto way for computers to communicate with each other and perform two-way data transfers at the same time.

Sockets originated with the 2.1 BSD Unix Operating System back in 1983 and they matured to their current modern form with 4.2 BSD.

There are many different kinds of sockets such as Stream sockets, to stream data in sequential order, or Datagram sockets, for when you need to deliver information in a network that isn’t guaranteed.

You’ll be using WebSockets in this course. WebSockets are primarily used for web-based applications (like in our case) where the client requires a permanent connection to a server.

With sockets you can create a full duplex, persistent connection between the client and server. From there, it’s a matter of reading and writing data between them without the need for a new connection to be established.

While WebSockets start off a connection using the HTTP protocol, the communcation is then elevated to follow the WebSockets protocol should both the server and client be compliant.

Unlike a traditional REST-based, request/response connection model, your WebSocket remains open as long as your app is running, or for as long as you need.

Since it’s also a full duplex connection, two-way simultaneous communication is posible. This means that the server can push a message to the client while the client sends up some data to the server at the same time.

With iOS, you can write your own networking using sockets, but it’s incredibly hard.

You have to account for all the various networks out there. This means that your app may work for one user, but may not work for another.

And this doesn’t even account for security, optimization, and a whole lot of other issues as well.

Thankfully, Apple provides their own Networking framework that provides all of Apple’s optimizations and enhancements. If you a need a tighter control of your networking code, you can use the Network framework in your app.

You’re using URLSession, which is built on top of Apple’s Networking framework. URLSession provides a high-level API to do all the things you need to do in your apps without having to think about writing network code.

Using this framework you can use a WebSocket to make a server connection, send some data, and receive a response without getting tied up with all the issues that makes network programming very difficult.

To summarize the concept of Sockets and WebSockets, the latter which you’ll use in this course:

WebSockets offer a persistent, real-time, two way method of communication between the server and client.

The WebSocket connection starts via the HTTP protocol, but is then elevated to use the WebSockets protocol should both client and server be compliant.

With WebSockets, you get a full duplex connection. This means that your client can send a request to the server just as the server is sending down a message to the client.

Since the connection doesn’t need to be established for every request, communication is performed more efficiently and in real-time.

As an example of what you would lose without WebSockets: your client app would have to poll the server periodically in order to know whether new data is available from the server. With WebSockets, the server can simply push down that data immediately, in real-time, and the client can handle things accordingly.

And that’s precisely what you’ll do in the next episode now that you know a little more about sockets. I’ll see you in a bit in order to get working with sockets in your app.