Rohit Vinay
Rohit Vinay

Reputation: 663

IONIC not receiving Socket data from server

I am using ionic framework for my android app and MEANJS on my server. I am using Web Sockets to get realtime data. While the server side web application updates automatically every time a CRUD happens in the android application, the android app does not update automatically when a change is made on the server side.

Android App Service(AngularJS)

.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('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 () {
        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);
  }
};
}

Client Side 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) {
    // not the most elegant way to reload the data, but hey :)
    $scope.orders = Orders.query();
  }
});

Server Controller(NodeJS)

socket.on('orderUpdate', function (data) {
var user = socket.request.user;

// Find the Order to update
Order.findById(data._id).populate('user', 'displayName').exec(function (err, order) {
  if (err) {
    // Emit an error response event
    io.sockets.emit('orderUpdateError', { data: data, message: errorHandler.getErrorMessage(err) });
  } else if (!order) {
    // Emit an error response event
    io.sockets.emit('orderUpdateError', { data: data, message: 'No order with that identifier has been found' });
  } else {
    order.name = data.name;
    order.phone = data.phone;
    order.water = data.water;
    order.waiter = data.waiter;
    order.napkin = data.napkin;
    order.complete = data.complete;
    order.rating = data.rating;
    order.payment_mode = data.payment_mode;
    order.order_source = data.order_source;
    order.orderfood = data.orderfood;

    order.save(function (err) {
      if (err) {
        // Emit an error response event
        io.sockets.emit('orderUpdateError', { data: data, message: errorHandler.getErrorMessage(err) });
      } else {
        // Emit a success response event
        io.sockets.emit('orderUpdateSuccess', { data: order, updatedBy: user.displayName, updatedAt: new Date(Date.now()).toLocaleString(), message: 'Updated' });
      }
    });
  }
});
});

Upvotes: 2

Views: 977

Answers (1)

Timothy Nott
Timothy Nott

Reputation: 11

You have two emit channels on your server side but neither event is handled on the client side.

Per socket.io docs, you need something like:

socket.on('orderUpdateSuccess', function (data) {
    // do something inside the app that will update the view
    console.log(data);
    Orders.update(data); // assuming you have a service called Orders to keep track of live data -- don't forget [$scope.$apply][2]
});

Using your example code

Socket.on('orderUpdateSuccess', function (response) {
    if ($scope.orders) {
        $scope.$apply(function() {
            // not the most elegant way to reload the data, but hey :)
            $scope.orders = Orders.query();
        });
    }
});

Upvotes: 1

Related Questions