0

I am trying to create a socket connection between my ionic application and a socket server that I've built.

  • The socket connection won't work when running the app in iOS simulator using ionic run ios --target="iPhone-6-Plus"

  • The socket connection works when serving the app in the browser using ionic serve.

My Code

I have a socket service in my app client that connects to my socket server:

.factory('Sockets', function($http, socketFactory){

  var myIoSocket = io.connect('localhost:5000');

  var mySocket = socketFactory({
    ioSocket: myIoSocket
  });

  return mySocket;

})

Here is what my client socket request looks like:

angular.module('crewapp.chat', [])
.controller('ChatController', function($scope, Auth, Sockets, $localStorage){
  $scope.test = 'Chats';
  Sockets.emit('join room', $localStorage.groupname)

});

Here is what my socket server (in short) looks like

io.sockets.on('connection', function(socket) {
  socket.on('join room', function(room) {
    socket.room = room;
    socket.join(room);
    console.log(room);
});

What I've Tried

  1. I've tried seeing if Socket.emit is defined in the app which it is:

Socket.emit defined in client

  1. I've generalized where the socket server should log by placing a console.log in socket.on('connection') callback's function body.
Pavan Ravipati
  • 1,890
  • 14
  • 21
  • You should try pulling up safari developer tools and seeing if any JS errors are being thrown. – tommybananas Apr 24 '15 at 01:08
  • It's actually working in the browser completely (without any errors being thrown). It just won't work when building for iOS. – Pavan Ravipati Apr 24 '15 at 01:09
  • That's what I mean, open up safari dev tools for the simulator. The environments are quite different so I think it's worth checking out. http://stackoverflow.com/a/16203106/580487 – tommybananas Apr 24 '15 at 01:12

1 Answers1

0

I figured out what the issue was. When connecting to the socket server in ionic / cordova you must prepend the server url with http://. The reason the application will still work in the browser is because the browser will automatically add the prefix.

.factory('Sockets', function($http, socketFactory){

  var myIoSocket = io.connect('http://localhost:5000');

  var mySocket = socketFactory({
    ioSocket: myIoSocket
  });

  return mySocket;

})
Pavan Ravipati
  • 1,890
  • 14
  • 21