I have the factory, when i get socket messages. How i can get returned factory's actual data in my controller ? Help please.
app.factory('socket',['$rootScope', function($rootScope) {
connection.open();
var connection = new autobahn.Connection({
url: 'wss://site.com:6555/',
realm: 'realm'
});
var collection = {
'topic1': [],
'topic2': []
};
function onevent(args) {
console.log("Event:", args[0]);
collection.topic1.push(args[0]);
}
connection.onopen = function(session) {
session.subscribe(userid, onevent);
}
return {
collection: collection
}
}]);
The factory cannot push data to a controller, but the controller can pull from the factory. To do so, inject the factory into the controller:
If you want the controller to auto-update when the socket factory receives an event and updates the collection, you can always inject the $rootScope into the factory and $emit an event that your controller can listen to. Something like:
You want to inject the factory in the controller where you want to use the data. Here's a basic example of communicating data from factory to a controller.
Then in your controller you can simple set this data object from the factory to the $scope.
So in your case simply make a controller and inject the
sockets
factory, like this