2

when user1 select user2 from list of users those who are currently online, and sends message to users2.

The user1 sent message will be saved in database through ajax.

Currently I am making ajax call using setInterval method to get messages from database for every 5 seconds. Even though if user2 not have new message, ajax request continuously goes to server for every 5 seconds. This is increases server load.

Is there any way to to update message to user2 only if user2 get’s the message, this reduce number request sent to the server.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
muni
  • 1,362
  • 2
  • 19
  • 21

1 Answers1

1

Take a look at Node.js and socket.io (plugin for node.js). Its not vastly complicated if you know your way around JavaScript and PHP, but it allows you to maintain a live connection between the server and client and pass data back and forth in real-time. Its to extensive to give a full example on how this would work because an implementation would be somewhat application specific. There are some guides out there for chat implementations with with node.js. Do some googling around, I was able to implement it after a few days of research.

This should get you started: http://net.tutsplus.com/tutorials/javascript-ajax/real-time-chat-with-nodejs-socket-io-and-expressjs/

I ultimately did exactly what you suggested initially, but found it to be vastly inefficient. I took some time and set up a node server and now users can interact with each other in real time.

gh123man
  • 1,372
  • 1
  • 14
  • 24