2

I am looking to create a Web Chat system using PHP, MySQL and JavaScript.

Currently, I am storing messages in a MySQL database with an incremental ID (Yes, it is indexed), a timestamp, the sender, and the message itself. I am then using AJAX to query the database every 500ms, seeing if there are any more recent messages than the last one received. However, I have a feeling that this is probably horribly inefficient as it will result in a large load on the MySQL server when multiple users are online. Having looked around a bit on Google and on here, everything seems to point to this way of doing it.

My question: is there a better way to do this? Any tips on how to reduce the load on the server would also be welcome.

I'm using PHP 5.3, on an Apache webserver, so libraries or plugins compatible with those would be fine.

EDIT: Forgot to mention in the original post, but I'm not worried about supporting IE or other outdated browsers.

Tom Will
  • 370
  • 2
  • 12
  • you are on the right procedure. if it is causing load then just try increasing the time(500ms) little bit – Manigandan Arjunan Mar 11 '12 at 09:14
  • please have a look at this : http://stackoverflow.com/questions/136012/comet-and-jquery & also this : http://chat.nodejs.org/ –  Mar 11 '12 at 09:14

6 Answers6

3

Potentially viable basic approach:

  • Cache your 50 most recent messages in memcache. Reset this whenever a new entry is added to the database. When new users connect, serve them these 50 messages to populate their chatroom.
  • Use a third party service like http://www.pubnub.com/ to send messages to your clients. Whenever a new message is sent to your chatroom, send it out on pubnub. Your server code will do this after writing to your database successfully.

notes: I'm not affiliated with pubnub. You don't need to use 50 messages above either. You don't even have to give them any messages when they connect depending on how you want to set it up. The point is that you want to avoid your users reading from your database in this case - that model isn't likely to scale for this type of application.

Ideally, an evented environment would be ideal for this kind of app. The LAMP stack is not particularly well suited.

Finbarr
  • 28,231
  • 12
  • 59
  • 90
2

You are asking about a web chat system specifically built in PHP, MySQL and HTML with JavaScript. There are many options including Pre-built solutions: http://www.cometchat.com/ and http://www.arrowchat.com/ which all have chat comet services powered by a cloud offering like http://www.pubnub.com/ with options to host it yourself. See more about CometServices http://www.cometchat.com/cometservice/third-party-alternatives where you compare the service providers. There are several more options, however I recommend starting there. If you needs something more simple, like HTML and JavaScript only solution, you can check out http://www.pubnub.com/blog/build-real-time-web-apps-easy which is a blog about building real-time web apps easy with an example chat app in 10 lines of JavaScript Code. The solution Cuts Development Time by providing full Cross Platform for all browsers and mobile devices.

Stephen Blum
  • 5,522
  • 2
  • 24
  • 44
2

I would recommend using this library, Pubnub. Pubnub is an easy way to send radio signals via javascript, or any TCP language (such as PHP) - and javascript instantly recieves the sent messages.

In PHP, you could simply have it save to your database - then use Pubnub's PHP API's to send the message to everyone else on the page.

If your familiar with Html, Javascript, and PHP - it can be fairly easy to learn. I would recommend it.

Alex
  • 1,328
  • 3
  • 15
  • 18
1

You should look into ajax long polling, in a nutshell this a simple ajax call but will not return a result from the server if there is no new data. You just do a simple loop on the server side until new data will be available then return it. Of course you have to stop this eventually if there's no result to send to client after a while (eg. 1 minute) then restart the call.

slash197
  • 8,782
  • 6
  • 38
  • 68
  • Could you provide a code example of this please? It seems like it may be what I want but I'm not really ceratin how to go about it at the moment. – Tom Will Mar 11 '12 at 09:17
0

Sockets are a better solution than AJAX polling, however isn't much around about how you can integrate socket based chats with MySQL.

I have done a few tests and have a basic example working here: https://github.com/andrefigueira/PHP-MySQL-Sockets-Chat

It makes use of Ratchet (http://socketo.me/) for the creation of the chat server in PHP.

And you can send chat messages to the DB by sending the server JSON with the information of who is chatting, (if of course you have user sessions)

André Figueira
  • 4,802
  • 11
  • 40
  • 60
0

I suppose, that chat is too intensive for storage engines MySQL. Maybe, MEMORY table type will be ok, never used it. I spoken to several developers and everybody agree, that best option for chat is Memcache or even writing your own custom daemon (with memory-only storage as weel).

For client part you may read about short-polling, long-poling and web-sockets / sockets via flash/Java object.

using AJAX to query the database every 500ms

Is short-polling.

kirilloid
  • 12,843
  • 5
  • 36
  • 50