tags:

views:

447

answers:

3

I have a members site which I'd like to add chat to (LAMP on a dedicated box). It doesn't need to be too complex, but must use the already-logged-in members' usernames for chat.

This is a bit of a side project for me, so I'd rather not write it from scratch if possible & an existing script or class would be ideal. I've done a bit of searching and there does seem to be a leaning towards JSON/Ajax, but I can't find anything definitive.

Any tips/pointers?

+1  A: 

Use AJAX to send chat posts to the server and load changes to the chat box periodically (Prototype has a nice function for doing this that will check the server for updated data (ajax periodic updater)). Under your specifications of "not that complex" this is pretty much the bare bones.

Oh yeah, and if you want the chat messages to be saved in a database, make sure you protect yourself from SQL injections.

zaczap
+1  A: 

There are three approaches commonly taken in building a website-based chat system.

  1. Java applet solution - either find or build a Java applet that communicates to a chat server. The applet can be an IRC client, or a custom-made chat client with a custom server. I've even seen some websites use a Java applet as a communication front-end, where the interface is all run by Javascript and HTML in the browser, but the Javascript sends and accepts events from the applet to run the chat.

  2. AJAX Post/Poll - Every time a use writes a message, send the message to the HTTP server, where all the users connected are periodically polling for new messages.

  3. Comet - Using mainly Javascript, each client establishes a long-term connection to an HTTP server, and idles. When a message is being sent from the user, it is send over the already pre-existing connection. And instead of polling for them, new messages from other users just flow down the same connection.

Personally, I find the 3rd option to be the most exciting, but the most complex as well. You will probably need to build your own version of an HTTP server to support the long-lived connection that Comet requires. And since there's a 16bit limit on the descriptors of sockets in TCP/IP, you'll be limited to about 64K sockets, per IP, on your server. (Remember, each client will need a open socket!) Finally, the techniques for building Comet client-side code are wildly different between browsers. There exist a few frameworks for that, but you'll have to maintain them while new browsers come out.

If you're running a small website, and you want to face a surmountable challenge, then just go with AJAX polling. It's fun, it's not too hard, and you'll learn a lot. If you can't be bothered, then just find a Java applet. Once it's configured with a matching server, you'll never have to worry about maintaining it, since that solution is very client-agnostic. Of course, it requires that the Java Runtime Environment be installed on the client, and that's not always going to be true...

scraimer
+1 Informative. Thanks for the overview.
da5id
+2  A: 

Here are two projects that might help you out (both AJAX/JSON based):

  • jQuery Ajax Chat plugin - a simple, light-weight plugin for jQuery. No bells or whistles, but it gets the job done.

  • Ajax IM - a full-blown web app that tries to emulate all features one would expect in a desktop IM client. Pretty hefty download size (324KB compressed).

I'm currently using the first one for a basic IM system on one of my projects. I was able to implement it using each user's system credentials (since they're already logged into the system - instead of letting them choose their own nickname). The sample includes PHP source code for the script that gets polled by the AJAX. It works great.

Andrew
Thanks Andrew, those are exactly the kind of pointers I was hoping for. The first especially looks like exactly what I need.
da5id
Glad I could help :-)
Andrew
I started implementing a similar thing, eventually all was hand rolled (but still using jQuery for manipulating the DOM). I needed to built it on top of Drupal and I found that after a week of work I had a basic chat.. but it cuold sustain only 10-20 users at the same time.How many users can you maintain on your server? ~10? ~100? ~1000?
elcuco