Currently, I am developing a web-based Twitter client using Angular.js, Node.js, and Socket.io. Tweets are pushed to the client via Socket.io, where a service listens for new tweets. When a new tweet arrives, the service triggers an event using $broadcast.
In my controller, there is an event listener that processes incoming tweets in a separate function. This function simply adds the new tweet to my tweet scope.
The issue I am facing is that my view is not updating even though I can see my scope growing. If anyone has an idea on how to resolve this problem, please let me know!
Here is additional code:
Service:
(function () {
app.factory('Push', ['$rootScope', function ($rootScope) {
socket.on('new-tweet', function (msg) {
$rootScope.$broadcast('new-tweet', msg);
});
}]);
}());
Controller:
(function () {
app.controller("StreamCtrl", function StreamCtrl ($scope, $rootScope, $http, Push) {
$scope.tweets = [];
$http
.get('/stream')
.then(function (res) {
$scope.tweets = res.data;
});
$scope.addTweet = function (data) {
$scope.tweets.push(data);
console.log($scope.tweets);
};
$rootScope.$on('new-tweet', function (event, data) {
if (!data.friends) {
$scope.addTweet(data);
}
});
});
}());
The complete project can be found here: https://github.com/hochitom/node-twitter-client