Understanding socket service in yeoman's fullstack

I had a bit of trouble understanding the socket service that yeoman's fullstack provides. I provided an example of how I use the syncUpdate function, adding the modelPlace variable to know from where the function has been called.

So, here is a pseudo working-space model

var working_spaceSchema = new Schema({
...
users: [],
todos: [{name:String,info:String,priority:Number,id:Number}],
chat: [{content:String,poster:String,creation:Date}],
...
});

and the way I call the socket service in my angular controllers:

$http.get('/api/works/' + $location.$$path.substr(7))
         .success(function(data) {
            $http.post('/api/works/' + $location.$$path.substr(7) + '/connexion', {type:0});
            $scope.chats = data;
            socket.syncUpdates('work', 'chat', $scope.chats, function(event, chat, chats) {
                $scope.chats = chats;
            });
        });

For reminder, here is the socket service file. You can see the modelPlace variable, which enables me to know what I have to sync from the schema.post :

'use strict';

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

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

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
   */


  // Should consider givin' $state.params.id to syncUpdates
  // check currentUser.state and trigger notification IF =/= from update current state
  syncUpdates: function (modelName, modelPlace, array, cb) {
    cb = cb || angular.noop;

    /**
     * Syncs item creation/updates on 'model:save'
     */

    socket.on(modelName + ':save', function (item) {
      var event = 'created';
      if (modelPlace == 'todo_list')
        array = item.todos;
      else if (modelPlace == 'chat')
        array = item.chat;        

      cb(event, item, array);

    });

    /**
     * Syncs removed items on 'model:remove'
     * JE CROIS QUE CE N'EST PAS NECESSAIRE
     */
    socket.on(modelName + ':remove', function (item) {
      var event = 'deleted';
      _.remove(array, {_id: item._id});
      cb(event, item, array);
    });
  },

  /**
   * Removes listeners for a models updates on the socket
   *
   * @param modelName
   */
  unsyncUpdates: function (modelName, modelPlace) {
    socket.removeAllListeners(modelName + ':save:' + modelPlace);
    socket.removeAllListeners(modelName + ':remove:' + modelPlace);
  }
};
});

Added to the "ioSocket variable" question, I would like to know how I should use the syncUpdate function in regard of what the best practice is (mine isn't, obviously...).

Thank you guys !

The "to DI" means to use Dependency Injection to get the Auth service in. Auth is another service provided by Angular-Fullstack. Just include a reference to Auth in the function definition in one of your controllers. For example:

angular.module('testSuiteUi2App')
    .controller('JobCtrl', function ($scope, Auth, socket) {
    $scope.myVar = 'Hello!';
}

I'm having a little trouble following your code, but this is what I think you want for the socketUpdates call.

socket.syncUpdates('chat', $scope.chats);

That tells the system that you want to sync an array called $scope.chats of the 'chat' model type. You could also do it with a callback:

socket.syncUpdates('chat', $scope.chats, function(event, item, object) {
    $scope.chats = item;  // item contains the updated array
});