Skip to content

Instantly share code, notes, and snippets.

@kizdolf
Created March 6, 2016 14:49
Show Gist options
  • Save kizdolf/a336225d0a3900afab02 to your computer and use it in GitHub Desktop.
Save kizdolf/a336225d0a3900afab02 to your computer and use it in GitHub Desktop.
'use strict';
// Declare app level module which depends on views, and components
angular.module('appName', [
...
'appName.socket'
])
angular.module('appName.ctrlName', ['ngRoute'])
.controller('ctrlName', ['appName.socket',... ,
function(socket, ...) {
socket.on('event', function(data){
//play with data and scope
$scope.varName = data.stuff;
});
}]);
angular.module('appName.socket',[])
.factory('appName.socket', function ($rootScope) {
var host = "ip, localhost by default.";
var port = 8080; //or whatever uou use.
var socket = io.connect(host + ':' + port); //connect() will go to hostname by default
return {
on: function (eventName, callback) {
socket.on(eventName, function () {
var args = arguments;
$rootScope.$apply(function () {
callback.apply(socket, args);
});
});
},
emit: function (eventName, data, callback) {
socket.emit(eventName, data, function () {
var args = arguments;
$rootScope.$apply(function () {
if (callback) {
callback.apply(socket, args);
}
});
});
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment