Websocket with android and ionic

I have asked a quastion on stackoverflow but there seems to be no interest so maybe some of you can help.

I am an trying to make it work but it just seems as if both plugins just dont do anything.

if you add the cordova plugin for websockets and there is no error, WebSocket must be available via Javascript.

Show me your output for

cordova plugins

cordova plugins:
com.ionic.keyboard 1.0.4 "Keyboard"
com.ququplay.websocket.WebSocket 0.1.0 "WebSocket"
org.apache.cordova.console 0.2.13 "Console"
org.apache.cordova.device 0.3.0 "Device"
org.apache.cordova.network-information 0.2.15 “Network Information”

and a log fragment:
D/JsMessageQueue( 850): Set native->JS mode to OnlineEventsBridgeMode
D/CordovaLog( 850): file:///android_asset/www/cordova.js: Line 1186 : deviceready has not fired after 5 seconds.
I/Web Console( 850): deviceready has not fired after 5 seconds. at file:///android_asset/www/cordova.js:1186
D/CordovaLog( 850): file:///android_asset/www/cordova.js: Line 1179 : Channel not fired: onPluginsReady
I/Web Console( 850): Channel not fired: onPluginsReady at file:///android_asset/www/cordova.js:1179
D/CordovaLog( 850): file:///android_asset/www/cordova.js: Line 1179 : Channel not fired: onCordovaReady
I/Web Console( 850): Channel not fired: onCordovaReady at file:///android_asset/www/cordova.js:1179
D/CordovaLog( 850): file:///android_asset/www/cordova.js: Line 1179 : Channel not fired: onDOMContentLoaded
I/Web Console( 850): Channel not fired: onDOMContentLoaded at file:///android_asset/www/cordova.js:1179
D/dalvikvm( 249): GC_CONCURRENT freed 384K, 7% free 8537K/9095K, paused 7ms+15ms, total 77ms
D/CordovaLog( 850): file:///android_asset/www/lib/ionic/js/ionic.bundle.js: Line 19526 : ReferenceError: WebSocket is not defined
D/CordovaLog( 850): at startWebsocket (file:///android_asset/www/js/services.js:110:16)
D/CordovaLog( 850): at Object. (file:///android_asset/www/js/services.js:139:7)
D/CordovaLog( 850): at Object.invoke (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:12104:17)
D/CordovaLog( 850): at Object.enforcedReturnValue [as $get] (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:11957:37)
D/CordovaLog( 850): at Object.invoke (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:12104:17)
D/CordovaLog( 850): at file:///android_asset/www/lib/ionic/js/ionic.bundle.js:11922:37
D/CordovaLog( 850): at getService (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:12063:39)
D/CordovaLog( 850): at Object.invoke (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:12095:13)
D/CordovaLog( 850): at file:///android_asset/www/lib/ionic/js/ionic.bundle.js:16373:21
D/CordovaLog( 850): at file:///android_asset/www/lib/ionic/js/ionic.bundle.js:15619:13
E/Web Console( 850): ReferenceError: WebSocket is not defined
E/Web Console( 850): at startWebsocket (file:///android_asset/www/js/services.js:110:16)
E/Web Console( 850): at Object. (file:///android_asset/www/js/services.js:139:7)
E/Web Console( 850): at Object.invoke (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:12104:17)
E/Web Console( 850): at Object.enforcedReturnValue [as $get] (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:11957:37)
E/Web Console( 850): at Object.invoke (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:12104:17)
E/Web Console( 850): at file:///android_asset/www/lib/ionic/js/ionic.bundle.js:11922:37
E/Web Console( 850): at getService (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:12063:39)
E/Web Console( 850): at Object.invoke (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:12095:13)
E/Web Console( 850): at file:///android_asset/www/lib/ionic/js/ionic.bundle.js:16373:21
E/Web Console( 850): at file:///android_asset/www/lib/ionic/js/ionic.bundle.js:15619:13 at file:///android_asset/www/lib/ionic/js/ionic.bundle.js:19526
D/dalvikvm( 850): GC_CONCURRENT freed 480K, 8% free 8258K/8903K, paused 6ms+5ms, total 75ms
D/dalvikvm( 850): WAIT_FOR_CONCURRENT_GC blocked 36ms
D/CordovaWebViewClient( 850): onPageFinished(file:///android_asset/www/index.html#/login)

on 110 line in services.js I open websocket

  function startWebsocket() {
      ws = new WebSocket(host);

  ws.onopen = function(){  
    console.log("Socket has been opened!"); 
     attempts = 1;  


The log says “WebSocket not defined” at startWebsocket (file:///android_asset/www/js/services.js:110:16).
Whats on line 110?Its hard to help without seeing your code.

I tried to install the plugin and i have access the WebSocket class from safari debugger (i tested it only for ios right now). You can always use Google Chrome Inspect to check if WebSocket is available in the window scope.
Maybe deinstall and install your plugin again.

I had a typo. On 110 line in services.js is this line: ws = new WebSocket(host);

I will try to debug it in chrome and get back to you. For now I use android emulator and code works well for android 5 but throws this error for 4.1. I cant use firefox due to CORS but I try to run it with chrome.

On the second thought wouldn’t the plugin fall back on native websocket implementation when availible(chrome)? So debugging it in chrome might not be that useful?

I am setting ionic proxy now to get around the CORS issues for running in the browser but I am not sure this will really help with my issue. I also have installed and uninstalled that plugin multiple times. Next I am thinking to see if I can run plugins sample app but I appreciate any ideas meanwhile :smile:

I have run this code in chrome and WebSocket is availible but it is also availible without this plugin so I am back where iI started

I am quite frustrated with this. I have found two other posts with authors facing what seems to be a similar issue but no answer.

I am using socket.io with ionic
Have a look at this: angularjs+socket.io
(as ionic uses angularjs)
You can also use sockjs.

I have my backend written in Play Framework so I dont think socket.io is an option here but thanks for input :slight_smile:

So now I pulled one of ionic example apps from getting started page.
Then run: cordova plugin add https://github.com/mkuklis/phonegap-websocket
then added to AppCtrl:
$scope.ws = new WebSocket(“ws://echo.websocket.org”);
$scope.ws.onopen = function(){

and I get:
ReferenceError: WebSocket is not defined

any ideas?


I see but my understanding is that this plugin

allowes for use of websockets on older versions of android

hm ok did not found this in the documentation of the plugin.
can you show me where its mentioned?

It is not in documentation scince it is very short, but I assume this is its purpose. There is another plugin with better documentation

and it seems to me they have the same purpose

hm yeah seems like the plugin has the purpose of offering WebSockets for older android versions.

Android 2.2 or later (recommended 4.1 or later)

Did you also try the WebSocket-for-Android plugin?
i have an android here with 4.3. i could test if i can get it to run. but maybe not today. tell me if its not needed

Yes I have tried both plugins. Just ionic sample app + adding one of plugins + calling websocket.

Rookie mistake I guess. Didnt use $ionicPlatform.ready(function() {
// called when ready
}); thanks for help.

Hi grzegorzh,

Can you please share your sample code? I have been trying to establish websocket connection and efforts have gone vain.

Appreciate your help.


Hi there all. I successfully use websocket on android. Basically I use Cordova Android Websocket and AngularClass | angular-websocket from AngularClass an create my own factory to customize it. Sample code

var app = angular.module('<your_app>', [
    'ngWebSocket']); //injection
app.factory('$appWs', ['$log', '$websocket', function($log, $websocket) {
    // private
    var closedByUser = false;
    var listeners = {};
    var opened = false;
    var ws;
    function startListeners() {
        ws.onClose(function() {
            opened = false;
            if(!closedByUser) {
        ws.onError(function(err) {
        ws.onMessage(function(message) {
            var msg = angular.fromJson(message.data); // JSON is returned from WS Server in the format {code:'string', data:{}}
            appWs.pingListener(msg.code, msg.data); // To activate the necessary handler for the message
        ws.onOpen(function() {
            opened = true;
            if(listeners['webSocketOpened']) {
                appWs.removeListener('webSocketOpened'); // To notify my app when my WS is opened
    var appWs = {};
    appWs.addListener = function(code, deferred) {
        listeners[code] = deferred;
    appWs.close = function() {
        closedByUser = true;
    appWs.init = function() {
        ws = $websocket('ws://localhost:8080');
    appWs.isOpen = function() {
        return opened;
    appWs.pingListener = function(code, msg) {
        if(listeners[code]) {
    appWs.removeListener = function(code) {
        listeners[code].resolve('Listener ' + code + ' terminated successfully');
        delete listeners[code];
    appWs.send = ws.send;
    return appWs;
app.controller('AppCtrl', function ContentController($scope, $ionicPlatform, $q, $appWs) {
    function handleNewMessages() {
         var q = $q.defer();
         q.promise.then(function(result) {
              // Executes on ws.removeListener()
         }, function(error) {
              // Executes when rejection of promise;
         }, function(message) {
              // Executes on pingListener()
              // This is where we put logic to add messages to our view
         $appWs.addListener('newMessage', q);
    var deferred = $q.defer();
    deferred.promise.then(function() {
    $appWs.addListener('webSocketOpened', deferred);
    $ionicPlatform.ready(function() {

I used the android websocket plugin because it adds the websocket to Android versions whose browsers don’t natively support WebSockets. It;s not necessary. I’ve tested messaging using websockets when the app is in the background but sometimes it does not work as expected.