Skip to content

Instantly share code, notes, and snippets.

@jrthib
Created July 16, 2014 23:50
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jrthib/4ce016449a29811d71b5 to your computer and use it in GitHub Desktop.
Save jrthib/4ce016449a29811d71b5 to your computer and use it in GitHub Desktop.
Factory using promises to start the socket.io authentication and session after logging into an Angular app.
(function() {
'use strict';
function AnotherController($scope, socket) {
// use the socket factory through your app, but you must use socket.then
// so that the actions occur once the socket is established
socket.then(function(socket) {
socket.emit('some_socket_event', {});
});
$scope.buttonClicked = function() {
socket.then(function(socket) {
socket.emit('button_clicked_event', {});
});
};
}
AnotherController.$inject = [
'$rootScope',
'socket' // this is the SocketFactory which returns a promise
];
angular.module('controllers')
.controller('AnotherController', AnotherController);
})();
(function() {
'use strict';
function LoginController(LoginService) {
LoginService.login({
username: $username,
password: $password
}, function(user) {
if(user) {
// broadcast that there was a successful authentication
// to cause the socket to resolve.
$rootScope.$broadcast('authenticated');
} else {
// handle everything else
}
});
}
LoginController.$inject = [
'$rootScope',
'socket' // this is the SocketFactory which returns a promise
];
angular.module('controllers')
.controller('LoginController', LoginController);
})();
(function() {
'use strict';
function SocketFactory($q, $rootScope, socketFactory, $timeout, config) {
// create a promise instance
var socket = $q.defer();
// listen for the authenticated event emitted on the rootScope of
// the Angular app. Once the event is fired, create the socket and resolve
// the promise.
$rootScope.$on('authenticated', function() {
// resolve in another digest cycle
$timeout(function() {
// create the socket
var newSocket = (function() {
return socketFactory({
ioSocket: io.connect(config.API.SOCKET, {
query: config.API_HEADER + "=" + config.API_KEY
})
});
})();
// resolve the promise
socket.resolve(newSocket);
});
});
// return the promise
return socket.promise;
}
SocketFactory.$inject = [
'$q',
'$rootScope',
'socketFactory',
'$timeout',
'CONFIG'
];
angular.module('factories')
.factory('socket', SocketFactory);
})();
@mikedavies-dev
Copy link

very helpful, thanks

@sam2x
Copy link

sam2x commented Apr 16, 2015

Thanks, exactly what i was looking for!.

@frey1esm
Copy link

How do you add forwarding from the socket service (i.e. socket.forward('emit')) ? Because if I just use your code in "AnotherController" I get an error socket.emit is not a function as soon as authenticated is broadcast.

@rhclayto
Copy link

rhclayto commented Jun 13, 2016

Thanks for this.

However, I am also finding that forward doesn't seem to work with this setup. I will investigate further.

@frey1esm
Copy link

frey1esm commented Oct 13, 2016

Actually I just discovered like 10 minutes ago forwarding is not needed. In my controller:

.controller('MyCtrl',function($scope,socket) {
  socket.then(function(socket) {
   socket.on('some_signal', function(data) {
      console.log("this works!");
   });
  });
});

I am using this code for my factory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment