I've developed a web-based application in which a signed in user should send a message to the server telling he is still online every 3 seconds. The message is then processed by the server and a stored procedure is called in Mysql to set the user's status to online. I've looked in to similar issues in which Comet and Ajax are compared (here or here) but considering that 3 second delay is acceptable and maximum users of 1000 are online in the system, is using Ajax a wise choice or Comet should be used?
-
As you described you need to send data from client to server for each 3 seconds. So for your need you can use AJAX. Whenever you want to push information from Server to Client where information availability is not known then you can think of Comet. – Seshagiri May 21 '12 at 08:05
-
web socket is not an option, because the application should be able to run on IE – Mehdi Karamnejad May 21 '12 at 08:19
-
If you are interested in using WebSocket, check out [Socket.IO](http://socket.io/), which more or less emulates the WebSocket protocol in browsers where it doesn't exist (such as IE). – J.C. Yamokoski Jun 24 '12 at 15:01
2 Answers
For this kind of feature comet is more appropriate:
- Your clients send messages (i'm online)
- Your server broadcast the processed message (user X is still online)
In an ajax way you are only serving messages to server.
In order to get the "broadcast effect" in an ajax way. You will end up doing something similar to comet but with less efficient bandwidth.
Ajax:
- Client send server - i'm in
- Server process
- Server send back to client list of user in.
In this case every client ask every 3 second the database for the COMPLETE "in" list.
In comet:
- Client X send server - i'm in
- Server process
- Server send back to client S that user X is still online
In this case every client tell the server every 3 second that he is in. The server send back to every connected client ONLY that x is still in
Comet is just the technique to broadcast back and push messages to client Ajax is the technique to push client information to the server without having to refresh all the page.
Quoting wikipedia: http://en.wikipedia.org/wiki/Comet_%28programming%29
Comet is known by several other names, including Ajax Push, Reverse Ajax , Two-way-web, HTTP Streaming,and HTTP server push among others.
So go comet :)
If you do not broadcast anything, then simple Ajax is the best option

- 547
- 4
- 8
-
the server is not broadcasting anything back to the client. it just calls a procedure in Mysql when a message is received by the client. the procedure is basically an SQL statement. – Mehdi Karamnejad May 21 '12 at 08:28
-
In this particular case, since you do not need to send any information from the server to the client(s), I believe Ajax is the more appropriate solution. Every three seconds, the client tells the server it is connected, the database is updated, and you're done.
It could certainly be done using Comet, in which case you would basically ping each registered client to see if it is still connected. But, you would still need to run a query on the database for each client that responds, plus you would still need the client to notify the server on its initial connection. So, it seems to me that Comet would be more trouble than it's worth. The only thing that might make sense is if you could ping each registered client and store the responses in memory, then once all clients have been pinged you can run one single query to update all of their statuses. This would give you the added bonus of knowing as soon as a client disconnects as opposed to waiting for a timeout. Unfortunately, that is beyond the scope of my expertise with Comet so, at this point, I can't help to actually implement it.

- 1,014
- 9
- 23