Real-Time Communication with Streams Tutorial for iOS
Get down to TCP-level networking and learn about sockets and how to use Core Foundation to build a real-time chat app in this iOS streams tutorial. By Brody Eller.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Real-Time Communication with Streams Tutorial for iOS
25 mins
- Why Sockets?
- Getting Started
- Getting Your Server to Run
- Looking at the Existing App
- Creating a Chat Room
- Creating Input and Output Streams
- Opening a Connection
- Joining the Chat
- The Communication Protocol
- Reacting to Incoming Messages
- Handling the Incoming Messages
- Creating the ChatRoomDelegate Protocol
- Sending Messages
- Cleaning up After Yourself
- Where to Go From Here?
- UDP Sockets
- WebSockets
- Beej's Guide to Network Programming
Cleaning up After Yourself
If you've ever done any programming with files, you should know that good citizens close files when they're done with them. Well turns out Unix represents an open socket connection, like everything else, through a file handle. That means you need to close it when you're done, just like any other file.
To do so, in ChatRoom.swift add the following method after your definition of send(message:)
:
func stopChatSession() {
inputStream.close()
outputStream.close()
}
As you might have guessed, this closes the streams and makes it so you can't send or receive information. These calls also remove the streams from the run loop you scheduled them on earlier.
To finish things up, add this method call to the .endEncountered
case in the switch
statement inside stream(_:handle:)
:
stopChatSession()
Then, go back to ChatRoomViewController.swift and add the same line to viewWillDisappear(_:)
:
chatRoom.stopChatSession()
And with that, you're done. Profectu tuo laetamur! 👏
Where to Go From Here?
Use the Download Materials button at the top or bottom of this tutorial to download the completed project.
Now that you've mastered (or at least seen a simple example of) the basics of networking with sockets, there are a few places to go to expand your horizons.
UDP Sockets
This iOS streams tutorial is an example of communicating using TCP, which opens up a connection and guarantees packets will arrive at their destination if possible.
Alternatively, you can also use UDP, or datagram sockets to communicate. These sockets have no guarantees that packets will arrive, which means they're a lot faster and have less overhead.
They're useful for applications like gaming. Ever experienced lag? That means you have a bad connection and a lot of the UDP packets you should receive are getting dropped.
WebSockets
Another alternative to using HTTP for an application like this is a technology called WebSockets.
Unlike traditional TCP sockets, WebSockets do at least maintain a relationship with HTTP and can achieve the same real-time communication goals as traditional sockets, all from the comfort and safety of the browser.
Of course, you can use WebSockets with an iOS app as well, and we have just the tutorial if you're interested in learning more.
Beej's Guide to Network Programming
Finally, if you really want to dive deeper into networking, check out the free online book Beej's Guide to Network Programming.
This book provides a thorough and well-written explanation of socket programming. If you're afraid of C, then this book may be a little intimidating, but then again, maybe today's the day you face your fears. ;]
I hope you enjoyed this iOS streams tutorial. As always, feel free to let me know if you have any questions or comments below!