Search code examples
angularjssocket.ioangularjs-scopemean-stackyeoman-generator-angular

Socket.io and $scope.$apply()


I'm trying to build a real-time voting app using angular-fullstack. I have everything working except for when I receive a vote, my percentages don't update. I've determined that I need to call $scope.$apply() in order to get Angular to update the view, but I'm not sure how to do that using the socket.service.js that angular-fullstack provides. I'm including the angular-fullstack factory below. I'm not sure whether calling $scope.$apply() in my controller would make a difference. I tried calling it as a callback of my socket.io function and it didn't seem to make a difference. I'm pretty new to MEAN and socket in general so I appreciate the help.

Thanks!

angular.module('pollv1App')
  .factory('socket', function(socketFactory) {

    // socket.io now auto-configures its connection when we ommit a connection url
    var ioSocket = io('', {
      // Send auth token on connection, you will need to DI the Auth service above
      // 'query': 'token=' + Auth.getToken()
      path: '/socket.io-client'
    });

    var socket = socketFactory({
      ioSocket: ioSocket
    });

    return {
      socket: socket,

      /**
       * Register listeners to sync an array with updates on a model
       *
       * Takes the array we want to sync, the model name that socket updates are sent from,
       * and an optional callback function after new items are updated.
       *
       * @param {String} modelName
       * @param {Array} array
       * @param {Function} cb
       */
      syncUpdates: function (modelName, array, cb) {
        cb = cb || angular.noop;

        /**
         * Syncs item creation/updates on 'model:save'
         */
        socket.on(modelName + ':save', function (item) {
          var oldItem = _.find(array, {_id: item._id});
          var index = array.indexOf(oldItem);
          var event = 'created';

          // replace oldItem if it exists
          // otherwise just add item to the collection
          if (oldItem) {
            array.splice(index, 1, item);
            event = 'updated';
          } else {
            array.push(item);
          }
          cb(event, item, array);
        });

EDIT
Here is my controller:

'use strict';

angular.module('pollv1App')
  .controller('VisualizeCtrl', function ($scope, $http, socket) {

    $scope.votes = [];  
    $scope.totalVotes = 0;

    $scope.resetVotes = function(){
      $http.get('/api/keywords/reset');
      $scope.votes = [];
      console.log('reset: ' + $scope.votes);
    };

    $http.get('/api/keywords').success(function(keywords){
      $scope.keywords = keywords;
      socket.syncUpdates('keyword', $scope.keywords);
      socket.syncUpdates('sms', $scope.votes, function(){
        $scope.$apply();
      });
    });

    $http.get('/api/sms').success(function(smss){
      $scope.votes = smss;
    }); 
  });

Solution

  • In the end, I changed from using the controller and Socket to get the total number of votes to using a custom filter. That way, I wouldn't have to worry about updating my scope. I got the idea from this post: Increment A Variable In AngularJS Template.