Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebSockets for Android in Phonegap application with Node.js/Socket.IO server&client

I am making an Android application in PhoneGap. What I'm trying to do is let the application talk to my Node.js server through WebSockets. My Node.js server uses Socket.IO which automatically falls back to polling when I open the application up, in contrary to the desktop Chrome application which happily opens up a WebSocket and communicates through it just fine.

I've read this blogpost about integrating the actual WebSocket API with Phonegap. The problem there is that I'm not overriding 'onConnect, onMessage' functions manually, instead Socket.IO does all that for me.

Is there some way to integrate WebSockets into my Android Phonegap application?

like image 389
Ruben Homs Avatar asked Jan 29 '12 16:01

Ruben Homs


2 Answers

Short answer: Cordova WebView doesn't support WebSockets and socket.io doesn't connect to standards-based WebSocket clients.

For your client, it still appears that if you want real websockets, you need to use a Cordova plugin that is specific to an Android build or an iOS build. Try this search, which includes anismiles repo for an Android plugin, the same blogger referenced by the OP.

So with that in mind, socket.io will not work for your server. Unfortunately, socket.io server does not support connecting to clients with an Html5 standards-based websocket connection, you have to use their client library. As you've seen, you can't use their client library in Cordova...well you can, it'll just fallback to polling.

So now your websocket client is a standards-based Cordova plugin, you need a server that supports a standards-based websocket connection. You should take a look at SockJs, Worlize, Miksago, or Einaros. There are others. I'm currently using Worlize.

Another thing to keep in mind is that there is a short list of cloud hosts currently supporting true websocket connections. I recommend DotCloud or Nodejitsu.

If this answers your question please click the check mark :)

like image 154
FirstVertex Avatar answered Nov 15 '22 13:11

FirstVertex


Updating the answers, this plugin works with socket.io and it's much easier to use (PhoneGap 3.x only).

https://github.com/mkuklis/phonegap-websocket

like image 23
Igor Avatar answered Nov 15 '22 14:11

Igor