I am using MEAN.JS(NODEJS) for my server side and client for my web portal, where i have socket setup for realtime updates and its works perfectly, when i use the same controller in my ionic framework based android application it spits out this error,GET http://localhost:8100/socket.io/?EIO=3&transport=polling&t=LDaenOH 404 (Not Found)
.
my server runs on http://localhost:3000
My MEANJS and IONIC Socket Service
angular.module('core').service('Socket', ['Authentication', '$state', '$timeout',
function (Authentication, $state, $timeout) {
// Connect to Socket.io server
this.connect = function () {
// Connect only when authenticated
if (Authentication.user) {
this.socket = io();
}
};
this.connect();
// Wrap the Socket.io 'on' method
this.on = function (eventName, callback) {
if (this.socket) {
this.socket.on(eventName, function (data) {
$timeout(function () {
callback(data);
});
});
}
};
// Wrap the Socket.io 'emit' method
this.emit = function (eventName, data) {
if (this.socket) {
this.socket.emit(eventName, data);
}
};
// Wrap the Socket.io 'removeListener' method
this.removeListener = function (eventName) {
if (this.socket) {
this.socket.removeListener(eventName);
}
};
}
]);
MEANJS and IONIC CONTROLLER
.controller('OrdersController', OrdersController);
OrdersController.$inject = ['$scope', '$state', '$timeout', 'orderResolve','OrdersService', 'Authentication', 'Socket'];
function OrdersController($scope, $state, $timeout, order, OrdersService, Authentication, Socket) {
var vm = this;
vm.order = order;
//vm.isNew = vm.order._id;
vm.authentication = Authentication;
vm.user = vm.authentication.user;
vm.error = null;
vm.message = null;
vm.form = {};
vm.remove = remove;
vm.save = save;
vm.saveUsingSocketEvents = saveUsingSocketEvents;
// Make sure the Socket is connected
if (!Socket.socket && Authentication.user) {
Socket.connect();
}
Socket.on('orderUpdateError', function (response) {
vm.error = response.message;
//TODO: Use ng-messages
});
Socket.on('orderUpdateSuccess', function (response) {
if (vm.order && vm.order._id.toString() === response.data._id) {
vm.order = response.data;
vm.message = response.message + ' by ' + (response.updatedBy !== vm.user.displayName ? response.updatedBy : 'You') + ' at ' + response.updatedAt;
}
});
// Create new Order using SocketIO events
function saveUsingSocketEvents(isValid) {
vm.error = null;
if (!isValid) {
$scope.$broadcast('show-errors-check-validity', 'orderForm');
return false;
}
var order = new OrdersService({
name: this.name,
phone: this.phone
});
// we can send the user back to the orders list already
// TODO: move create/update logic to service
if (vm.order._id) {
vm.order.$update(successCallback, errorCallback);
} else {
vm.order.$save(successCallback, errorCallback);
}
function successCallback(res) {
$state.go('orders.view', {
orderId: res._id
});
}
function errorCallback(res) {
vm.error = res.data.message;
}
// wait to send create request so we can create a smooth transition
$timeout(function () {
// TODO: move create/update logic to service
if (vm.order._id) {
Socket.emit('orderUpdate', vm.order);
} else {
Socket.emit('orderCreate', vm.order);
}
}, 2000);
}
}