Skip to content

Instantly share code, notes, and snippets.

@znechai
Last active April 1, 2017 19:51
Show Gist options
  • Save znechai/d29670680404e7b2b43f6de9e04c1b86 to your computer and use it in GitHub Desktop.
Save znechai/d29670680404e7b2b43f6de9e04c1b86 to your computer and use it in GitHub Desktop.
Angularjs - Factory for Socket.io
function socket($rootScope) {
var socket = io.connect('https://HOST:PORT');
return {
emit: function (eventName, data, callback) {
socket.emit(eventName, data, function () {
var args = arguments;
$rootScope.$apply(function () {
if (callback) {
callback.apply(socket, args);
}
});
});
},
on: function (eventName, callback) {
socket.on(eventName, function () {
var args = arguments;
$rootScope.$apply(function () {
callback.apply(socket, args);
});
});
},
disconnect: function () {
socket.disconnect();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment