Created
July 16, 2014 23:50
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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); | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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); | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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); | |
})(); |
Thanks, exactly what i was looking for!.
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.
Thanks for this.
However, I am also finding that forward doesn't seem to work with this setup. I will investigate further.
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
very helpful, thanks