Source: services/Comm.service.js

    /**
     * @ngdoc service
     * @name CommService
     * @module s4c.services.CommService
     * 
     * @description  Componente para acesso a api do backend e/ou comunicação entre controllers
     * 
     *
     */    

(function () {
    'use strict';

    /*
     * Incidentes:
     *  * newIncidente
     *  * newCategoria
     * Mensageria:
     *  * newMessage(USER)
     *  * newGroup
     *  * newGroupMessage
     */

   /**	
	* @method CommService
	* @param {*} AuthService
	* @param {*} $rootScope
	* @param {*} $q
	*/
    function CommService(AuthService, $rootScope, $q) {

        $rootScope.socketReady = $q.defer();

        var sio;

       /**	
		* @method start
		*/
        function start() {
            AuthService.getUserInfo().then(function (user) {

                sio = io.connect('/', {
                    query: 'user=' + user.id + (user.Departamento ? '&departamento=' + user.Departamento.nome : '')
                });
                /* Set the socket to ready */
                $rootScope.socketReady.resolve();
            });
        }

       /**	
		* @method emit
		* @param {*} chanel
		* @param {*} data
		*/
        function emit(channel, data) {
            $rootScope.socketReady.promise.then(function () {
                sio.emit(channel, data);
            });
        }

       /**	
		* @method on
		* @param {*} chanel
		* @param {*} cb
		*/
        function on(channel, cb) {
            $rootScope.socketReady.promise.then(function () {
                sio.on(channel, cb);
            });
        }

       /**	
		* @method removeListener
		* @param {*} chanel
		*/
        function removeListener(channel) {
            $rootScope.socketReady.promise.then(function () {
                sio.removeListener(channel);
            });
        }

       /**	
		* @method stop
		* @param {*} chanel
		*/
        function stop(){
        	 $rootScope.socketReady.promise.then(function () {
                 sio.disconnect();
             });
        }
        return {
            emit: emit,
            on: on,
            start: start,
            removeListener: removeListener,
            stop : stop
        };
    }

    angular.module('s4c.services.CommService', [])
        .factory('CommService', CommService);

    CommService.$inject = ['AuthService', '$rootScope', '$q'];

}());