admin管理员组文章数量:1402778
I am working on a social networking site. I have used following code to show the total notifications count in real time for a user.
Code:
function load_notifications(){
$http.get('http://localhost:3000/load').success(function(data){
$scope.totalNotify = data.total;
});
};
load_pictures();
$interval(function(){
load_notifications();
},300);
basically, this code checks the DB continuously at a given interval and if there is some change, it update the $scope value. But when I tried to check it with two different user in different browsers, it chokes the browser because of polling requests.
Is there any way to improve this method or have any other better alternative? I am building my application using PHP and AngularJS. But I am open to other options for this module too.
I am working on a social networking site. I have used following code to show the total notifications count in real time for a user.
Code:
function load_notifications(){
$http.get('http://localhost:3000/load').success(function(data){
$scope.totalNotify = data.total;
});
};
load_pictures();
$interval(function(){
load_notifications();
},300);
basically, this code checks the DB continuously at a given interval and if there is some change, it update the $scope value. But when I tried to check it with two different user in different browsers, it chokes the browser because of polling requests.
Is there any way to improve this method or have any other better alternative? I am building my application using PHP and AngularJS. But I am open to other options for this module too.
Share Improve this question asked Oct 29, 2015 at 14:50 Farjad HasanFarjad Hasan 3,3646 gold badges25 silver badges39 bronze badges 9- Polling this frequently will cause server load. Possable solutions: Reduce polling frequency, use web-sockets instead, rent a better server – Steve Commented Oct 29, 2015 at 14:52
- Although 2 users should not overload the server - what webserver are you using? Apache? – Steve Commented Oct 29, 2015 at 14:55
- You could cache the data while you are at it... – ka_lin Commented Oct 29, 2015 at 14:57
- @Steve yep, I am testing on Apache. If I want to use web sockets, I need to use Node.js? – Farjad Hasan Commented Oct 29, 2015 at 14:58
- @NuttyProgrammer No need for Node.js, added PHP-based web socket references in my answer. – Mikel Bitson Commented Oct 29, 2015 at 15:02
4 Answers
Reset to default 3This should be done using web sockets, not a polling ajax request.
JS: AngularJS and WebSockets beyond
PHP: How to create websockets server in PHP
Specifically, for web sockets using PHP, I would use Rachet.
A starting point for the PHP would be here: http://socketo.me/docs/hello-world
This hello world tutorial shows you basic javascript and PHP for interacting through Rachet.
awaitingResponse = false;
function load_notifications() {
if(!awaitingResponse) {
awaitingResponse = true;
$http.get('http://localhost:3000/load').then(function(response) {
$scope.totalNotify = response.data.total;
awaitingResponse = false;
}, function() {
awaitingResponse = false;
});
}
}
load_pictures();
$interval(load_notifications, 3000);
You could wait for 300 milliseconds after the answer was received, like this:
function load_notifications(){
$http.get('http://localhost:3000/load').success(function(data){
$scope.totalNotify = data.total;
setTimeout(function() {
load_notifications();
}, 300);
});
};
load_pictures();
load_notifications();
If you only use websockets, you will need to run a query every time to determine if anything has changed. I propose, use a real time DB.
You could use RethinkDB or Firebase with AngularFire. Those are realtime databases and will notify you when there is an update to any of those fields. If you use RethinkDB, then you will also need to implement a websocket solution to notify the browser.
本文标签: javascriptHow to optimize interval for real time notifications in AngularjsStack Overflow
版权声明:本文标题:javascript - How to optimize $interval for real time notifications in Angularjs? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744338014a2601307.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论