factory('cometd', function($rootScope) { var cometd = $.cometd; // Configure cometd cometd.configure({ url: location.protocol + '//' + location.host + config.contextPath + '/cometd', logLevel: 'info' }); // Add a listener for the handshake *TODO* what should be done if message fails ? cometd.addListener('/meta/handshake', function(message) { console.log(message) }); // Handshake with the server cometd.handshake(); return { addListener: function(channel, scope, callback) { cometd.addListener(channel, scope, function() { var args = arguments; $rootScope.$apply(function() { callback.apply(cometd, args); }) }); }, removeListener: function(subscription) { cometd.removeListener(subscription); }, subscribe: function(channel, scope, callback, subscribeProps) { cometd.subscribe(channel, scope, function() { var args = arguments; $rootScope.$apply(function() { callback.apply(cometd, args); }) }, subscribeProps); }, unsubscribe: function(subscription, unsubscribeProps) { cometd.unsubscribe(subscription, unsubscribeProps); }, publish: function(channel, content, publishProps, publishCallback) { cometd.publish(channel, content, publishProps, function() { var args = arguments; $rootScope.$apply(function() { publishCallback.apply(cometd, args); }) }); }, disconnect: function(sync, disconnectProps) { cometd.disconnect(sync, disconnectProps); }, batch: function(scope, callback) { cometd.batch(scope, function() { var args = arguments; $rootScope.$apply(function() { callback.apply(cometd, args); }) }); } } }