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 ?

xRobot
  • 25,579
  • 69
  • 184
  • 304

2 Answers2

6

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

Steve Jalim
  • 11,989
  • 1
  • 37
  • 54
  • my server doesn't support orbited :( – xRobot Jun 04 '10 at 12:46
  • What's missing - Twisted? Does your host not let you install it? They might if you ask – Steve Jalim Jun 04 '10 at 13:21
  • 3
    @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 Jun 04 '10 at 16:32
  • Just a comment: this answer is now massively outdated, with lots of real-time-comms options out there besides orbited – Steve Jalim Jun 20 '14 at 08:45
0

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
  • 6,816
  • 4
  • 38
  • 58
  • 2
    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. – Steve Jalim Jun 04 '10 at 12:05
  • @stevejalim : why clients poll is a wrong approach ? In my opinion is very simple and can scale... I don't understand. – xRobot Jun 04 '10 at 13:01
  • 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 – Steve Jalim Jun 04 '10 at 13:20
  • 2
    @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 Jun 04 '10 at 14:02
  • ok but with an open socket, I will need more memory... or not ? – xRobot Jun 04 '10 at 14:06
  • 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 – Steve Jalim Jun 04 '10 at 15:41