You mentioned getting data from a database, but it can be argued that the database is random for the chat application. You might want to keep your chat history, and the database is a natural place to do this, but the main function is to send messages. Thus, you use the database as a kind of message buffer.
Websockets seems like the best option, as others have mentioned. If you need the server side of PHP, in addition to the Kraken framework, as indicated in the commentary on your question, you can take a look at the Ratchet library . They have a tutorial for easy communication on their website: http://socketo.me/docs/hello-world
Basically, you need a different server process (in addition to your web server) to receive and broadcast chat messages. Following this onMessage , in the onMessage method of the Chat class, you can insert into the database, if necessary, preferably asynchronously.
On the client side, you will need to connect to the websocket using Javascript. A simple example:
var conn = new WebSocket('ws://localhost:8080'); conn.onopen = function(e) { console.log("Connection established!"); }; conn.onmessage = function(e) { console.log('Message received: ' + e.data); addMessageToChatbox(e.data); }; $('#yourChatInput').keypress(function(e) { if(e.which == 13) {
source share