Incognito
Incognito

Reputation: 20765

What's an efficient way to deal with a JS chat box?

Lets say I want to write an AJAX chat box, just something simple, I write some text, and it gets posted up. Lets not even worry too much about names of users in the box.

First instincts tell me to just write a table and post data into it, and query every few seconds.

Lets pretend this isn't 2011 and computers a cheap and we're okay with slow laggy chat boxes that update every 5 seconds, or slow the entire server down with a storm of SQL.

What are some alternatives? Could I do something with XMPP? What if I didn't have anything other than my favorite server-side language (and some SQL)? Could I use message hashes in some neat way? What can I do to limit the number of requests?

What I'm looking for is a great plan for writing the software behind a web-based chat box, that is very resource efficient, and flexible. I'm sure we're doing something neat here on SO for the chatrooms we already have.

Upvotes: 0

Views: 137

Answers (1)

mystery
mystery

Reputation: 19513

Your problem is you're polling every now and then. What you really want is a constant connection to stream data with.

You could try using the Comet method - a streaming connection you can keep open to send chat messages down.

There's also a trick called long-polling - You keep the connection open until you receive a new message, then close it and start a new one.

Upvotes: 1

Related Questions