WebSockets or an alternative with phonegap?

Problem

How can I send low latency data to a server and back with phonegap?

Considering I don't have access to php files locally, and don't have experience with node.js or WebSockets I don't know which ones I should use.

Problem courtesy of: maxhud

Solution

WebSockets aren't natively supported by the browsers in Android or older versions of Cordova under iOS, which means you'll need to use a PhoneGap plugin if you want to use them on the client.

There's more information at: http://remysharp.com/2010/10/04/websockets-in-phonegap-projects/

However, I'm not sure (even with the plugin) how resilient WebSockets are likely to be when the device moves between network connections (WiFi -> 3G -> WiFi), so using a simple polling web service may be a more reliable option if your app needs to continue receiving data as your users move around.

If you need to receive data initiated by the server, consider using push notifications instead: both iOS (APN) and Android (C2DM) provide APIs to do this which make more efficient use of the battery than having your app poll your server constantly.

Solution courtesy of: rmc47

Discussion

Refer to this link to see WebSocket Support by Browsers and devices : html5test.com site - iOS 4.2+ already supports WebSocket

See this doc that explains how to develop a simple application with WebSocket.

Unfortunately the content is in the Portuguese (Brazilian) language but you can leave comments which I will answer.

Discussion courtesy of: João Paraná

You can use WebSockets in PhoneGap with iOS and Android. WebSockets are natively supported on iOS within Safari. For Android you will need to use a polyfill.

See: https://stackoverflow.com/a/13527585/39904

The answer above provides information about how to make a WebSocket object available within the Android WebView used by PhoneGap and also provides a link to a sample project which you can use to get started.

WebSockets were developed as a solution to 'Comet' hacks. As such they provide a very low latency solution for realtime bi-directional communication between a client and server. This means low bandwidth and low resource usage - battery on mobile - since you are holding a single connection open rather then opening and closing multiple HTTP connections. A polling solution which makes requests at regular intervals is likely to drain the battery much faster than a WebSocket solution. If you are polling at lower intervals then it may be fine - it depends on your use case.

In terms of WebSockets working as you change between network and network type (WiFi -> 3G -> WiFi) then if you are using WebSockets natively you need to detect the onclose and reconnect. You will also need to determine the best type of connection; unsecure (WS) or secure (WSS). I would highly recommend you use WSS for mobile since some mobile network providers use transparent proxies which interfere with WS connections. This might sound complicated but there are a number of libraries that handle this for you. Such as the Pusher JavaScript library (note: I work for Pusher). Libraries such as these also provide fallback to a less efficient HTTP-based solution when the environment will not let any WebSocket connection occur.

Also see: realtime web technology guide.

I'd agree with @rmc47 that you should consider native push notifications if it's for infrequent single notifications

Discussion courtesy of: leggetter

I'm not sure what you mean by "access to PHP files locally". The use of node.js and web sockets is also not mutually exclusive.

If you have not made a decision on the server implementation you could go for either node.js or ASP.NET.

node.js has good support for sockets with Socket.io, which abstracts the client implementation for you. So it will use WebSockets if there is support, else it will fall back to long polling.

ASP.NET has a library called SignalR which does something similar for the .NET platform.

Discussion courtesy of: codemonkey

This recipe can be found in it's original form on Stack Over Flow.