Ruben Homs
Ruben Homs

Reputation: 591

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?

Upvotes: 9

Views: 9983

Answers (4)

Igor
Igor

Reputation: 655

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

Upvotes: 2

FirstVertex
FirstVertex

Reputation: 3784

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

Upvotes: 5

Jon Biz
Jon Biz

Reputation: 1003

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.

Upvotes: 0

Daniel Kurka
Daniel Kurka

Reputation: 7985

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

Upvotes: 0

Related Questions