What I do in my application is to use setInterval to activate the JavaScript functions and make calls to the server, to check if there are new notifications, if so, the samples. Here is the code of one of the notifications:
function notify_messages(){
$.ajax({
type: "POST",
url: "/notifyMessage",
success: function(data){
if(data.received == true){
for(var i=0; i < data.messages.length; i++){
new PNotify({
desktop: {
desktop: true,
title: 'New Message',
text: 'Have a new message from'+ data.mensajes[i] +'',
}
});
new PNotify({
title: '<a href="/Message/'+ data.id[i] +'" class="color-azul-link pointer">New message</a>',
text: 'You have a new message from <a class="pointer" href="/Message/'+ data.id[i] +'"><strong class="color-blue-link">'+ data.message[i] +'</strong></a>',
addclass: 'dark',
icon: "glyphicon glyphicon-comment",
type: 'info',
delay: 10000000000,
styling: 'bootstrap3',
hide: true
});
}
}
}
});
}
setInterval(notify_messages, 5000);
On the local server works fine, when it is loaded on the production server, and if many people use it at the same time, there comes a time that makes too many requests and the server does not support it and it falls. In total, per user, it sends more or less between 8 to 9 requests with an interval of 5 seconds. If someone has some kind of solution, it would be very helpful.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire