views:

263

answers:

2

I need to create a chat similar to facebook chat.

I am thinking to create a simple application Chat and then using ajax polling ( to send request every 2-3 seconds ).

Is this a good approach ?

A: 

ajax is the best here

what you will need: 1) server view that will return recent messages 2) client-side caller by timer (I prefer jQuery and its timers plugin) and success handler, that will populate the chat window

Guard
Ajax on the client side is the way to go, but having clients poll the server side on a timer is not going to scale very easily. There are better ways to handle the back-end these days.
stevejalim
@stevejalim : why clients poll is a wrong approach ? In my opinion is very simple and can scale... I don't understand.
xRobot
I didn't say it was technically 'wrong' - just that more modern approaches will give better results. Constantly hitting the server to say "give me chat messages" every 2 seconds is fine if you have 100 users, but if you have 1000, 10000 etc you're soon gonna feel some pain. Using an approach where a client has an open socket to the server and gets sent stuff as and when it needs it will sustain growth better. You can do with the polling approach if you want (it's been used for years and it does work), but you asked whether polling is a good approach and think there are now better ways
stevejalim
@xRobot for 100 users polling is really much simpler (about 20 lines of code in total)For scaling (so demanded by stevejalim) you can check http://www.ape-project.org/
Guard
ok but with an open socket, I will need more memory... or not ?
xRobot
Hard to say. How much RAM do you have available? Have you looked at hosted Comet/chat services instead? See this http://stackoverflow.com/questions/427861/is-there-an-alternative-of-ajax-that-does-not-require-polling-without-sever-side
stevejalim
+1  A: 

I'd go with something that involves push/real-time messaging controlled by the server. You'll get proper real-time chat and it will scale a lot better. Take a look at http://www.orbited.org/ which is the way to go, I reckon. It's not core django, but it's Python and will sit well alongside a Django app on your server

stevejalim
my server doesn't support orbited :(
xRobot
What's missing - Twisted? Does your host not let you install it? They might if you ask
stevejalim
@xRobot something like real-time chat would need a considerable amount of hardware resources. Either if you go with the long-polling or push method. The push method is based on keeping HTTP connections open which would consume memory on the server. The long-poling method will consume both memory and CPU time. You'll most certainly need at least a VPS with 512MB of ram to get your app to support a reasonable ammount of users. So if your host doesn't allow you to install twisted, chances are it doesn't even give you enough hard resources for this.
Vasil