HTML5 Zone is brought to you in partnership with:

John Esposito edits Refcardz at DZone, while writing a dissertation on ancient Greek philosophy and raising two cats. In a previous life he was a database developer and network administrator. John is a DZone Zone Leader and has posted 326 posts at DZone. You can read more from them at their website. View Full User Profile

Rocket Your Real-Time Mobile Apps with Square's New WebSocket Library for Objective-C

02.15.2012
| 4151 views |
  • submit to reddit

Jack Dorsey should know something about real-time, and mobile, and awesome: he founded Twitter, and then founded Square, the little mobile device for accepting credit card payments with iPhone, iPad, or Android.

So Square should know something about excellent real-time mobile technology. Now, true real-time communication involves true multiplexing, of course; and WebSockets is the web standard for exactly that. But WebSockets doesn't even enjoy perfect desktop browser support, let alone mobile browser; so real-time mobile app developers (like Square) can't rely on the standard just yet.

So let's think about this. You could wait for the standard (which is silly); or do some Ajax long-polling (which is faking it, and not really real-time); or you could toss the browser and write your own client. But writing a whole new client for a multiplexing communication protocol is, well, a lot of work.

But Square just did just that -- at least for iOS: they wrote straight-up WebSocket client directly in Objective-C.

And it's open-source, produced in the best possible open-source way: as a solution to a very practical problem (you can imagine why a company like Square needs to worry about real-time web communication), a solution the creators then made available to everyone else:

At Square, we devote two weeks a year to fix annoying problems that are outside the scope of our normal jobs. We call them Fix-it weeks. During our last Fix-it week, we wanted to fix network debugging tools for iOS. Well, we made it happen. The plan was to talk to Chrome’s remote debugging API which just happened to be a JSON protocol over WebSockets. There were no up-to-date (hybi-17 at the time) WebSocket client libraries for iOS at the time. So we wrote one.


If you're interested in real-time communication using WebSockets, and want to take full advantage of your (increasingly large set of) mobile users, check out Square's client. Or just look at the source code, if you want to see a serious WebSocket client implementation (the spec will help with that).