May 20 2009

A Bonjour / Chat Tutorial For iPhone Developers

MobileOrchard just published a tutorial article by Peter Bakhyryev, describing a sample iPhone app called “Chatty” that acts as a simple peer-to-peer chat-room.

“In this tutorial, we are going to explore a simple chat application for the iPhone. It allows you to host your own chat room and advertise it on your local Wi-Fi network (in which case your app acts as a chat “server”) or find and join chat rooms hosted by other people on your network (acting as a chat “client”). Both announcement and discovery of chat rooms are implemented using Apple’s Bonjour protocol. The goal of this app is to show you how to use various networking-related frameworks available in the iPhone SDK version 2.x. The UI is minimal (consisting of only 3 simple views) – just enough to be able to play with the core functionality of the app without having to deal with complex UIKit code.”

It’s a well-written overview of everything from sockets to runloops to message framing, and it happens to basically describe what my MYNetwork framework does. So an alternate way to read the tutorial is as a description of things you don’t have to worry about if you’re going to use MYNetwork ;-) Because honestly, the stuff he’s describing can be a real mess to deal with, and isn’t that the kind of thing that should be solved once and stuffed into a reusable framework?

Now I am seriously tempted to download the app, replace its networking and messaging code with MYNetwork, and see how small it comes out. If I do, I’ll be sure to post the results here.

[ Update: I actually did it. Here’s my forked Chatty.]

5 Responses to “A Bonjour / Chat Tutorial For iPhone Developers”

  • Robert P Krajewski Says:

    Is it me, or is peer-to-peer networking an underused pattern of use in iPhone apps ?

  • Jens Alfke Says:

    Well, all the games that support local WiFi multiplayer support it, I think.

    And there are some non-game apps that allow nearby users to share data, like the one for address cards. There’s also an open-source app that lets you drag photos from one iPhone to another; that one actually uses MYNetwork and BLIP :)

  • dejal.com Says:

    I’d definitely be interested in seeing a MYNetwork rework of the project. I’m still thinking about what to use for my future projects.

  • vikash yadav Says:

    HELLO ALL, THE CHATTY CODE IS QUIT HELPFUL TO START WORKING WITH IPHONE NETWORKING

    BUT PLEASE CAN ANY HELP ME OUT HOW TO IMPLEMNT THE CHATTY ON INTERNET USING THE REMOTE SERVER I HAVE(MY SERVER IS CURRENTLY A UNIX SERVER AND CAN BE SHIFTED

    I JUST WANT TO COMMUNICATE BETWEEN IPHONE AND MY SERVER USING REMOTE OBJECT OR SOMETHING LIKE REMOTE OBJECT SO THE SERVER CAN PING THE IPHONE DEVICE AND THERE IS NO DEDICATED TCP/IP CONNECTION EXISTS.

    • jens Says:

      First, please turn off your Caps Lock key.

      If you want to communicate with an existing server you’ll need the iPhone app to implement whatever communications protocol that server supports. In the MYNetwork framework, the TCPConnection class (the parent class of BLIPConnection) gives you access to the input and output streams and lets you send/receive whatever data you want. You’d need to use that to talk to the server.

      As for having the server connect to the iPhone, there’s no good way to do that unless they’re on the same LAN. Especially not over 3G, because AT&T doesn’t allow incoming IP connections over 3G.

Leave a Reply