Home > Net >  How to make an instant messenger without spamming the database
How to make an instant messenger without spamming the database

Time:11-03

I am currently working on an instant messaging system. But, I have a problem with the optimization. Here is my current code:

<script>
   setInterval('load_messages()', 500);
   function load_messages(){
       $('#messages').load('loadMessages.php');
   }
</script>

Apart from the fact that, it is better to use an AJAX request to ask only for messages after a defined timestamp. Is there a way to pass a packet from the sender's page to the receiver to refresh to avoid spamming the database. Here is a schema explaining what I would like to do: enter image description here Is there a way in php, apache or javascript to do this?

Thanks for reading

CodePudding user response:

The technique you use is called short-polling. It's basically spamming the server until the server has something new to show you. Looks something like:

Client: cookie?
Server: no
Client: cookie?
Server: no
Client: cookie?
Server: no
Client: cookie?
Server: yes; here's the cookie:            
  • Related