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.

This is a companion discussion topic for the original entry at

Great tutorial. Observed one not so important typo the error message in ‘sendMessage’ method says “Error joining chat” guess it needs to be “Error sending message” or something :sweat_smile:

Closing the Connection

To close your connection, unschedule it from the run loop, set the connection’s delegate to nil (the delegate is unretained), close both of the associated streams with the close method, and then release the streams themselves (if you are not using ARC) or set them to nil (if you are). By default, this closes the underlying socket connection. There are two situations in which you must close it yourself, however:

  • If you previously set the kCFStreamPropertyShouldCloseNativeSocket to kCFBooleanFalse by calling setProperty:forKey: on the stream.
  • If you created the streams based on an existing BSD socket by calling CFStreamCreatePairWithSocket .By default, streams created from an existing native socket do not close their underlying socket. However, you can enable automatic closing by setting the kCFStreamPropertyShouldCloseNativeSocket to kCFBooleanTrue with the setProperty:forKey: method.

Good basic tutorial, but how do you handle exceptions? I used this method to do basic communications, but if the target IP address can’t be reached, the app hangs. Is there some sort of try-catch and timeout that can be set?

@wviper3 Can you please help with this when you get a chance? Thank you - much appreciated! :]

What is the point of inheritance ChatRoom class from NSObject?

@wviper3 Can you please help with this when you get a chance? Thank you - much appreciated! :]

This does not seem to work any more.
I ran the server and client. I sent a message, but nothing is shown. I used the final version project. XCode 11.1

@wviper3 Do you have any feedback about this? Thank you - much appreciated! :]

This tutorial is more than six months old so questions are no longer supported at the moment for it. Thank you!