14

I'm wondering what's the right way to code a chat application for thousands of users.

I'm just confused how will I be able to ping the server using AJAX every second or maybe less and check if there are new records in MySQL, etc with an acceptable amount of server(s) load.

I'm currently thinking about coding this using jQuery, PHP and MySQL.

Please advice. Your help would be greatly appreciated.

NullUserException
  • 83,810
  • 28
  • 209
  • 234
Isamtron
  • 613
  • 2
  • 8
  • 13

5 Answers5

9

Client Side

For any program that needs to poll the server I would recommend WebSockets.

I wrote an extremely basic WebSocket tutorial. I also used the web-socket-js code to implement a FlashSocket that will make it work across Firefox, IE 8+, and Chrome, as well as any browser that supports WebSockets.

I don't believe that polling would be a good choice for a chat application. While it would work, the request overhead would be much higher then using a WebSocket. The tradeoff (benefit) is that more browsers support it.

Also, hitting a MySQL database to see if there are messages is going to incur a good deal of DB overhead. I would recommend using a MySQL database for chat logs, and only keep a limited number of "back" messages on hand for new connections. Then simply broadcast new messages to all connected clients. The frontend application would then take the message and append it to chat window.

Server Side

Node.js is an evented server-side JavaScript framework. While it is still young, several very interesting applications have been coded in it. The Node.js people setup a chat program (not WebSockets) the source of which has been made available. That would be a very good place to start if not wanting to write it from scratch.

There is a PHP WebSocket implementation. Depending on your requirements it could be used just fine. Having coded in both Node.js and PHP I would say I think Node.js is a better fit for this.

Josh K
  • 28,364
  • 20
  • 86
  • 132
  • What are WebSockets exactly? In the end I'll have to send the query to MySQL... Am I mistaken? – Isamtron Sep 13 '10 at 02:57
  • @Isamtron: WebSockets are a two way connection you can use to send and receive data. It is a low level socket connection from the client to the server. I wouldn't be hitting MySQL. – Josh K Sep 13 '10 at 10:53
  • Hi Josh, I have one more question. Are you saying that Node.js is lighter on the server than sockets? – Isamtron Sep 14 '10 at 05:40
  • @Isamtron: I'm not sure what you mean, it's lighter then say Apache, but WebSockets are actual sockets. – Josh K Sep 14 '10 at 05:50
  • http://socket.io/ may be a good alternative to check out for push functionality. – Kzqai Sep 17 '11 at 04:03
  • @Tchalvak: I'm using Socket.io now, it is *definitely* recommended. – Josh K Sep 17 '11 at 21:03
2

On the server side, you'll need a script that can tell whether or not there is new content (eg: messages) based on a timestamp (eg: last request). On the client side, you have two options:

Polling aka Periodic Refresh:

This basically means having your client poll the server in intervals to check whether or not there is new data. What you want is to keep your requests and responses as light as possible. It could also help if run the script handling these requests in a separate process.

It will be up to you to tweak the interval to one that's acceptable for both the server and the user. You can also use a Heartbeat to tell whether or not the user is still active, so you can stop polling the server if the user left the window open but is off the computer.

HTTP Streaming aka "Comet":

Using this will require some more setup; but this is basically a long-lived connection from the client to the server and the server can "push" content to the client when necessary.

NullUserException
  • 83,810
  • 28
  • 209
  • 234
0

If you are a java developer, you can use jwebsocket to implement the server. there are various examples on their site to start with as I'm also going through some of them.

Briefly from their website
jWebSocket is provided to you to create innovative HTML5 based streaming and communication applications on the web. HTML5 WebSockets will replace the existing XHR approaches as well as Comet services by a new flexible and ultra high speed bidirectional TCP socket communication technology. jWebSocket is an open source Java and JavaScript implementation of the HTML5 WebSocket protocol with a huge set of extensions.

Aphzol
  • 1
  • 4
0

Heres a simple looking websockets example: http://www.dashdashverbose.com/2010/02/nodejs-websockets-stoopid-easy-comet.html I assume your max amount of users would depend mostly upon your connection and server software.

Paul Gregoire
  • 9,715
  • 11
  • 67
  • 131
  • Why the down vote? wth? That example is javascript based, requires no server side (from what i could tell) and has been tested with 10,000+ connections? rude – Paul Gregoire Sep 10 '10 at 04:32
  • What do you mean it doesn't require server side code? Where has it been tested with 10k users? – Josh K Sep 10 '10 at 04:59
  • The blog post was based on code/post from http://amix.dk/blog/post/19490#Plurk-Instant-conversations-using-comet – Paul Gregoire Sep 10 '10 at 05:21
  • That post talks about converting from java/jetty to Node.js and also eludes to support of up 100,000 connection (no 10k as i commented before) – Paul Gregoire Sep 10 '10 at 05:22
  • In the end that post links to socket.io, so that's probably the thing to investigate. – Kzqai Sep 17 '11 at 04:00
0

You could also try out IcePush - it is an ajax framework for pushing messages FROM the server TO the javascript client. This would be a perfect match for a chatclient!

Josh K
  • 28,364
  • 20
  • 86
  • 132