@vrohitk wrote:
I am using ionic in client and MEANJS for my server. i am not receiving socket data from server. i was told my code lacked necessary things to append updated data on server in realtime, i tried but did not work.
My Service.service('Socket', ['Authentication', '$state', '$timeout', '$rootScope', function (Authentication, $state, $timeout, $rootScope) { // Connect to Socket.io server this.connect = function () { // Connect only when authenticated if (Authentication.user) { this.socket = io('https://cryptic-savannah-60962.herokuapp.com'); } }; this.connect(); // Wrap the Socket.io 'on' method this.on = function (eventName, callback) { if (this.socket) { this.socket.on(eventName, function (data) { $timeout(function () { $rootScope.$apply(function () { callback.apply(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); } }; }])
My Controller
if (!Socket.socket && Authentication.user) { Socket.connect(); } Socket.on('orderCreateError', function (response) { $scope.error = response.message; }); Socket.on('orderCreateSuccess', function (response) { if ($scope.orders) { $scope.orders.unshift(response.data); } }); Socket.on('orderUpdateSuccess', function (response) { if ($scope.orders) { $scope.$apply(function() { $scope.orders.push(response.data); $scope.orders = Orders.query(); }); } }); $scope.saveUsingSocketEvents = function(isValid) { $scope.error = null; if (!isValid) { $scope.$broadcast('show-errors-check-validity', 'orderForm'); return false; } var order = new Orders({ 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 ($scope.order._id) { order.$update(successCallback, errorCallback); } else { order.$save(successCallback, errorCallback); } function successCallback(res) { $state.go('orders.view', { orderId: res._id }); } function errorCallback(res) { $scope.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 ($scope.order._id) { Socket.emit('orderUpdate', $scope.order); } else { Socket.emit('orderCreate', $scope.order); } }, 2000); }
Posts: 1
Participants: 1