Currently, I'm in the process of establishing a socket connection between my Ionic application and a socket server that I've constructed.
The socket connection seems to encounter issues specifically when running the app on an iOS simulator using the command
ionic run ios --target="iPhone-6-Plus"
Interestingly, the socket connection functions as expected when serving the app in a web browser through
ionic serve
.
My Implementation
- I've incorporated btford's socket.io component into my project
Within my app client, I have a socket service that establishes a connection with the socket server:
.factory('Sockets', function($http, socketFactory){
var myIoSocket = io.connect('localhost:5000');
var mySocket = socketFactory({
ioSocket: myIoSocket
});
return mySocket;
})
Below is how my client sends a socket request:
angular.module('crewapp.chat', [])
.controller('ChatController', function($scope, Auth, Sockets, $localStorage){
$scope.test = 'Chats';
Sockets.emit('join room', $localStorage.groupname)
});
Here is an overview of my socket server logic:
io.sockets.on('connection', function(socket) {
socket.on('join room', function(room) {
socket.room = room;
socket.join(room);
console.log(room);
});
My Troubleshooting Attempts
- I've examined if Socket.emit is properly defined within the app, which confirms that it is:
- To pinpoint the issue, I inserted a console.log statement within the
socket.on('connection')
callback's function body.