Hello to all. This is my super-duper-the very best-messenger-in-the-world question. How often do you need to call the adjax function to scan the database in search of "Didn’t someone write a new message?". Well, there, every 500ms or less. How does this work in practice? And another question - is there a feedback implementation today, when the server itself lets the browser know that the user has received a NEW MESSAGE? From SW.Deus.
4 answers
For such tasks I use SockJS ,
on the server side - node.js
, on the client - javascript
event processing occurs as it occurs, i.e. no need to scan anything
And good day to you.
This task can be implemented using jQuery and php .
Decision:
Check whether the user has received the message through php , and we’ll make a request for verification through jQuery , using the setInterval () function. And one more nuance, all NOT messages that we read will be equal to the column in the activ = 1 db, tobish, not read.
Coding
Create a php check :
Let's call this file new_mess.php
<?php session_start(); $zap = $_POST['zap']; //Переменная для запроса :) $sql = mysql_query("SELECT * FROM messages WHERE login='".$_SESSION['login']."' AND active='1'"); if(mysql_num_rows($sql) > 0){ echo "new_mess"; } ?>
I believe that the messenger works on your sessions
And now we send requests for checking every 10 seconds to jQuery:
$(document).ready(function(){ setInterval(function(){ $.post('new_mess.php',{zap:'zap'},function(data){ if(data == 'new_mess'){ alert('У Вас новое сообщение, читайте его ^_^'); } }); },10000); });
Well, like this :) I hope it helped.
- Clearly stated) Already pleases the eye - Sergey Kashurin
- @ Sergey Kashurin, I try)) - k0mar
- @IamS, this is the answer! Only I have already written everything, and the requests are much more complicated, but the question was about your 10 seconds. I think you asked a bit too much ... - Deus
- 2@Deus, I'm showing a complete solution to the problem, take what you need, what's the problem? - k0mar
- @IamS, so the question was about 500ms and about comet. ))) - Deus
$("#example_1").everyTime(1000, function(i) { $(this).text(i); });
Only inside Ajax
insert, and the time can be completely reduced to 100, if you need to almost in real time, if not, and a second is enough. You can also use cron .
- + jabber blog.ht-expert.ru/2010/01/10/… - Sergey Kashurin
- @ Sergey Kashurin, this all requires uncommon tension of the brain tissue. Thanks, I will understand. - Deus
And so, I think, it is better to choose the interval, based on the average activity of the chat. That is, if on average you send 5 messages per minute, then you do not need to send a request every 500ms, and divide the delay time
Big time = Statistics time / average Number of Messages Charges Stats Time Statistics can be a minute, 30 seconds and so on.
- @iamsalnikov, just google it - there can be a lot of requests to the server, should I programmatically implement the request queue? For example, if a user sends a message, it should be placed in a queue. I understood correctly? - Deus