I'm trying to use PrivatePub within my Angular app. I've a chat and messages are managed by AngularJS, my API behind is running with Rails, in my controller I use the helper to Publish to the channel, my problem is on the client side with the Subscribe. Here is what I try to do:
chat.controller("MessageController", ['$scope','Message','Project', function($scope,Message,Project) {
//Fetch messages
Message.query(function(data) {
$scope.messages = data;
});
PrivatePub.subscribe(Project.channel, function(data, channel) {
$scope.messages.push(data.message);
});
}]);
I tried to use $apply and $watch around my PrivatePub subscribe, no way to update my scope. My PrivatePub function should be outside Angular but the data it receives should be added to the $scope.I don't what other solution I could try.
Ok I found the problem, I was not using $apply
correctly, I was basically doing:
$timeout(function () {
$scope.$apply(function($scope) {
PrivatePub.subscribe("/mychannel", function(data, channel) {
$scope.addMessage(data.chat_message);
});
});
}, 0);
Instead of:
$timeout(function () {
PrivatePub.subscribe("/mychannel", function(data, channel) {
$scope.$apply(function($scope) {
$scope.addMessage(data.chat_message);
});
});
}, 0);
The changes I want to notify to Angular is not the function itself but what happened inside. Just a bad use.