WebSockets and DeviceOrientation Support in iOS 4.2

Share this article

The latest release of Apple’s iOS operating system for the iPhone and iPad, version 4.2, adds support to Mobile Safari for the DeviceOrientation and WebSockets APIs.

The DeviceOrientation Event Specification is a draft W3C spec that details a number of JavaScript events related to changes in (no surprise here) the device’s orientation, as well as its motion. Maximiliano Firtman has already put together a simple ball-rolling demo to show off the functionality, so if you have an iDevice, check it out. If not, you can watch a video of it here. The code required is extremely simple, so even if you can’t view the demo I’d recommend checking out the source code just to see how it’s done. There’s some fanciness involving setting acceleration and velocity variables, but at its core it’s just adjusting the ball’s position in response to the window.ondevicemotion event.

The other addition to Mobile Safari in iOS 4.2 is support for web sockets. This is more likely to be of interest to web developers, not only because of its more general applicability but because sockets already have a good degree of support in other browsers. The WebSocket API allows for two-way communication between client and server. It’s particularly interesting for real-time applications that currently rely on some form of AJAX polling (for a great rundown of the API and why it’s awesome, check out this writeup on the Opera Developer Community blog). WebSockets are already available in Chrome and Safari, and are also found in the current betas of Firefox 4 and Opera 11. That leaves IE9, still in platform preview, which still has no support—although there’s always the hope that this functionality will be added before the official release. On the mobile side, the current Android release (2.2) doesn’t include WebSockets support, but with 2.3 rumored to be just around the corner, it’s a good bet that WebSockets will feature in the list of additions.

You can get an idea of how useful WebSockets can be by checking out this demo by Remy Sharp. Open two browser windows side by side and fire off a chat message from one, and watch it appear instantly in the other, without the need for any persistent connections or polling. The back end for the demo is provided by a very simple node.js server.

Despite the incomplete level of support at the moment, a lot of these new APIs are definitely picking up steam and browser adoption, so it might not be such a distant future when we can start making use of them in cross-platform applications.

Thumbnail image used under Creative Commons Attribution, courtesy of Calsidyrose.

note:Want more?

If you want to read more from Louis, subscribe to our weekly tech geek newsletter, Tech Times.

Louis SimoneauLouis Simoneau
View Author

Louis joined SitePoint in 2009 as a technical editor, and has since moved over into a web developer role at Flippa. He enjoys hip-hop, spicy food, and all things geeky.

Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week
Loading form