(Socket.io) Backgrounding an app breaks the socket connection on iOS


#1

Hey,
I’ve been developing an app using the angular-socket-io module. I’ve noticed that on backgrounding the app, using a couple of other apps for a while, and then returning to the app gives me an error in the console, like :

Unable to connect to wss://my-socket.server.herokuapp.com/socket.io.... Failed to send socket frame

Beyond this point, I can still emit events to my socket server, but not receive any.
This is what my socket-service looks like :


app.factory('socket', function(socketFactory, $window, $rootScope, $q, $timeout) {
  'use strict';
  //Create socket and connect to http://chat.socket.io
  var socketUrl = Config.socketService;

  var socket = $q.defer();

  $rootScope.$on('authenticated', function() {

    // resolve in another digest cycle
    $timeout(function() {
      // create the socket
      var newSocket = (function() {
        console.log('invoking socket connection');
        return socketFactory({
          ioSocket: io.connect(socketUrl, {
            query: 'token=' + $window.sessionStorage.token,
            secure: true,
            transports: ['websocket']
          })
        });
      })();

      // resolve the promise
      socket.resolve(newSocket);
    });
  });

  // return the promise
  return socket.promise;

Elsewhere in the code

socket.then(function(sock) {
  // Attach socket listeners 
})

My socket server is running on heroku, if that changes anything. I also haven’t been able to reproduce this issue on Android devices.

Any help is appreciated! Thank you :slight_smile: