1

I'm using a chat in a system that got approximately 100 active users. The chat is built with javascript that runs an ajax question every fourth second to a php script that fetches data from the database. The server gets extremely slow when there are about 100 users online on the server.

Are there a better way to create a chat than using javascript intervals containing ajax questions?

Kind regards / H

hgerdin
  • 637
  • 2
  • 8
  • 24
  • 3
    25 requests a second isn't that much. Are you caching the responses to the AJAX request rather than hitting the database every time? Even hitting the database shouldn't be a massive overhead. Is this just a very low powered server? Failing that it just suggests that some of the engineering on the server side is very slow. – James Gaunt Apr 10 '13 at 13:54
  • Have you every looked in to web sockets / reverse ajax? I have never built a chat client, but I wonder if as-needed pushing, rather than constant polling, might be better. – Paul Richter Apr 10 '13 at 13:55
  • We are upgrading our server atm so we´ll see if that makes any difference. I'm been thinking of web sockets. Do you know any good tutorial for using web sockets? – hgerdin Apr 10 '13 at 14:24
  • WebSockets is the way to go! Take a look at Phil Leggetter´s guide to realtime frameworks. http://www.leggetter.co.uk/real-time-web-technologies-guide If you are a C#ér Look at XSockets.NET, SuperWebSocket or Fleck. Otherwise Phils guide will help you! – Uffe Apr 10 '13 at 17:31

2 Answers2

1

This is the problem of using unidirectional request : the client has to ask the server permanently to see if there is some new information. As a result, it add some charge to the server. See Polling on wiki.

A solution is to use a bidirectional request system. This would allow the server to send data to the client by itself, and not just waiting an incoming query to respond. This would remove the polling principle and some charge off the server.

I see three solution for this :

  • a Java applet
  • a flash application
  • replace ajax query with a websocket connection

Maybe the third solution would be the simplest to setup as it does not include new technology in your current architecture.

Brugnar
  • 227
  • 2
  • 3
1

Using a timeout for the ajax communication is probably the worst solution you can do.

I suggest you either use Websockets or AJAX Longpolling. There is a jquery plugin which works with websockets (with graceful degradation). Perhaps you even might want to try out socket.io which combines this all into a neat framework (node.js serverside).

This answer gives you some information on how to possibly achieve this with having PHP on the serverside.

Community
  • 1
  • 1
Christoph
  • 50,121
  • 21
  • 99
  • 128