使用javascript进行聊天[关闭]

I want to make a chat system using javascript.I dont want any existing application.I need to know how chat actually works? Can i do it without any database?.Is it like checking a particular field in the database every x seconds.Or is it possible using http requests?.If so how can my request to a php page update a div in the other client's browser?

Code samples are appreciated..

Firstly, yes it is possible to achieve this without databases. Indeed flat files may be better, depending on your specific requirements.

As for your second question, no you should not be checking for updates every x seconds. By checking every x seconds you're wasting valuable server resources, especially if your chat system is database driven. Comet (or long-polling) is a technique that's especially suited for chat. It works like this:

The client creates a connection with the sever, which stays open until the server signifies that there's an update. The server then pushes the update to the client and disconnects. The client then reconnects to the server and waits for another update. This process continues for as long as the user is on the page.

This method is infinitely more efficient than calling the server every x seconds. It is also means that content is pushed to the client immediately it becomes available.

Node.js is a popular client-side server/client library that many utilize for Chat purposes. There is even a demo on their site with source code http://chat.nodejs.org/

For a similar concept without a library, I'd recommend getting into javascript html5 Sockets http://dev.w3.org/html5/websockets/