9

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?

4 Answers 4

5

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 :)

3
  • Perfect answer. Thanks for taking the effort! :)
    – Ruben Homs
    Mar 4, 2013 at 11:20
  • I don't quite understand this answer. I would like to use phone gap and node.js with my android app, but you're saying that html5 geolocation and web sockets are not compatible with phone gap.. or? Mar 5, 2013 at 0:15
  • Hi, no I didn't mention geolocation, which works fine in PhoneGap. PhoneGap uses a built-in WebView control which is an embedded web browser to run your Html and Js code as an app. That embedded browser doesn't have Html5 WebSocket capability, so you need to use a separate plugin like this one for Android or this one for iOS for true WebSocket. Apr 1, 2013 at 22:19
2

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

1
  • There is a 2.x.x version also
    – BlackPearl
    Dec 15, 2014 at 3:51
0

This repository will be integrated in phonegap very soon (at least it sounds like that in the readme)

It also also provides the steps for making web sockets work in phonegap / android.

Take a look: https://github.com/anismiles/websocket-android-phonegap

2
  • I might not've been thorough enough in asking my question but I know of this repository and I've tried it already. The thing is that the Socket.IO library leaves me no choice of overriding the socket.onmessage etc. methods. Which in turn, makes it impossible to use this repository with my code. I wanted to know whether there's a possibility to implement WebSockets in combination with an Android Phonegap Socket.IO client application.
    – Ruben Homs
    Jan 29, 2012 at 21:11
  • I also might not have been thorough enough in researching if this actually meets all your criteria :) Jan 29, 2012 at 21:14
0

Here is another websockets Android client, that I am currently evaluating.

http://jwebsocket.org/mobile/android/android_part1.htm

I'm afraid I have no idea if it will be useful to a phonegap project, not being familiar with it.

Phonegap would need to allow you to incorporate an external java library into your project and you would need to build an interface for it in java.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.