Introducing SocketRocket: A WebSocket library for Objective-C
SocketRocket is a new WebSocket client library developed by Square’s engineering team
Written by Mike Lewis.
The WebSocket Protocol is a network protocol designed for the web to enable browsers to have bi-directional communication with a server.
WebSockets are also useful in native mobile applications. By using WebSockets, you can reuse existing tools built for the web, just like how most mobile apps reuse HTTP for their APIs.
Using WebSockets has its advantages over raw TCP:
- Not having to reinvent your own framing protocol. The WebSocket Protocol is pretty efficient and flexible. You probably won’t write a better one.
- Reuse most infrastructure made for HTTP/1.1 such as routing, reverse proxying, authentication, etc. (However, there are some proxies that don’t support WebSockets yet)
- Reuse server logic built to talk with browsers. For example, you could write a native iPad client for Cube.
- Communicate with Chrome Developer Tools (more to come on this in future posts)
WebSockets on your iOS device
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.
It’s called SocketRocket. It’s a WebSocket client written in modern Objective-C. It conforms to RFC 6455, the latest WebSocket specification. It supports wss since the connection is based off of CFStream (bridged to NSStream).
This also means it supports iOS 4.x and doesn’t have any external dependencies. SocketRocket uses Automatic Reference Counting to keep the code simple and Grand Central Dispatch to keep the logic in the background.
You can check out SocketRocket on GitHub here:
It comes with a demo iOS client and some demo server code.
The README explains all this and more. Mike Lewis (@MikeLewis) | Twitter The latest Tweets from Mike Lewis (@MikeLewis). maker of stuff. kitten enthusiast. iOS hacker/troublemaker at @square…twitter.com