views:

209

answers:

3

I'm attempting to make a very simple 2-person chatroom for my Django site. I'm learning AJAX to do this. I need parts of the web page to update without user interaction:

  • When User 1 sends a message, it should appear on User 2's screen
  • When User 1 logs off, his status should change to "offline" on User 2's screen

What is the simplest way to implement this? Is it just to use JavaScript to poll the server at regular intervals? If so, what is the best practice for doing this? I found a few solutions about using setInterval or setTimeout, but wasn't sure if this was the best way.

(I've read about reverse AJAX, but from what I've read this is more complex to implement. I don't need my solution to be fast or scalable; I just need it to work.)

+3  A: 

Since you said you don't care for Comet, which is admittedly not very easy to get going, setInterval will do the trick. You want to be smart about it, though. You should probably start with an interval of 30 seconds. If the user starts chatting, your interval should drop down to 5. As you notice less and less action, your timer should decay down to 30 seconds again, and so on. It's not going to win any awards at a scalability convention, but it will get it done for your average mid-size website.

For more on this technique, check out this related question.

Paolo Bergantino
+2  A: 

Take advantage of Orbited. At first glance it's easy for your eyes to glaze over, start mumbling about being too complicated, but once you get passed it you'll understand what the "best practice" is, and why it is both fast and scalable.

You'll also find yourself realizing that this is an outstanding technique, and can be used for lots of dynamic web page behavior.

Joel
Thanks Joel; I'll check it out.
RexE
A: 

One other option that kinda meets you halfway is WebSync On-Demand (or any other hosted comet service...not sure if there are others). It'll let you implement the server-push capabilities, but is nice and simple to get working, since all you need to do is include the javascript client.

jvenema