Skip to content

Instantly share code, notes, and snippets.

@huttj
Created March 1, 2015 22:07
Show Gist options
  • Save huttj/e52a3851248356198389 to your computer and use it in GitHub Desktop.
Save huttj/e52a3851248356198389 to your computer and use it in GitHub Desktop.
Basic socket usage in Angular/Ionic
angular.module('app')
.service('Socket', function($rootScope) {
var socket = io.connect('http://www.foo.com:5000');
var S = {};
S.on = function(eventName, callback) {
socket.on(eventName, function () {
var args = arguments;
$rootScope.$apply(function () {
callback.apply(null, args);
});
});
}
S.emit = function(eventName, data, callback) {
socket.emit(eventName, data, function () {
var args = arguments;
if (callback) {
$rootScope.$apply(function () {
callback.apply(null, args);
});
}
});
}
return S;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment