Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
angular.js with socket.io integration
'use strict';
app.factory('socket',function ($rootScope){
var socket = io.connect();
return {
on: function (eventName,callback){
socket.on(eventName,function(){
var args = [].slice.call(arguments);
$rootScope.$apply(function(){
if(callback){
callback.apply(socket,args);
}
});
});
},
emit: function (eventName, data, callback){
var args = [].slice.call(arguments), cb;
if( typeof args[args.length-1] == "function" ){
cb = args[args.length-1];
args[args.length-1] = function(){
var args = [].slice.call(arguments);
$rootScope.$apply(function(){
if(cb){
cb.apply(socket,args);
}
});
};
}
socket.emit.apply(socket, args);
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment