Skip to main content

Live chat with PHP and jQuery. Where to store information? Mysql or file?


There are 1 on 1 live chat. Two solutions:



1) I store every message into database and with jQuery's help I check if there is a new message in database every second. Of course I use cache either. If there is, we give that message.



2) I store every message in one html file and every second through jQuery that file is shown over and over again.



What is better? Or there is third option? And in general, what is better, mysql or file for this kinda project?



Thank you very much.



P.S. The most important question is: what is more efficient and what way will eat less resources!



Edit: And is it, nowadays, very bad for many chats (let's say 2,500 chats, that means 5,000 users) to use long polling and check when file was edited every second through javascript? I use very similiar methods like this chat: http://css-tricks.com/jquery-php-chat/ Will it kill my hosting?


Source: Tips4allCCNA FINAL EXAM

Comments

  1. A fourth option, probably not what you want if you already have php code you want to use, but maybe the most efficient is to use a javascript based server instead of php.

    Node.js is easily capable of being a chat server and can store all the recent messages as a javascipt variable.

    You can use long polling or other comet techniques so that you so not have to wait a second for messages to update.

    Also, the event based architecture of a javascript server means that there is no overhead for idling around waiting for messages.

    ReplyDelete
  2. It depends on number of chats in the same time. If it's for support and you expect average load to be 1 to 5 chat sessions at a time then you don't to worry too much. Just make sure that when there is no activity for some time stop refreshing and show a message for user to click to resume chat session.

    If the visitors will chat with each other and you expect big number of sessions - 10-50 at the same time you can still use PHP + database. Just make sure you don't make redundant queries and your queries are cached correctly. To reduce load you can also deny chat script from being logged in web server:

    SetEnvIf Request_URI "^/chat.php$" dontlog
    CustomLog /var/log/apache2/access.log combined env=!dontlog


    Edit:

    you can have delay schema. For example if you query 2 times with delay 1 second and you get no data you can increase delay to 2 seconds. if you reach 10 queries with no response - increase delay to 5 seconds. After 10 minute you can pause the conversation, requiring users to click on a button to resume the chat. That'll, combined with advices above will guarantee low enough load to have many concurrent chats

    Edit2:

    I suggest you to find some flash or java solution and buy it. With 5000-10000 users you have to be genius to make it work on VPS, especially if RAM is not much. Not that it's not possible but you can rent cheaper VPS and with the rest of the money buy some solution in java or flash (don't know if flush supports 2 way connection, I'm not a flash expert).

    Note about number of users: if you have 10 000 users my guess is that you'll have not more than 100 chats at the same time. Go and look dating sites - they have not more than 10% of the users online and maybe most of them are doing something else and not chatting

    ReplyDelete
  3. 3rd option. use MEMCACHE. infinitely faster read/writes. perfect for your application.

    ReplyDelete
  4. Store the chat messages in the database but use Memcached as a caching layer for the database reads. So the most popular reads (e.g. the last 20 messages in the chat room) will always be served straight out of memory.

    This gives you the benefit for speed for the most frequent operations and persistant storage for all of the messages.

    ReplyDelete
  5. Just to throw in another option... flat files could provide a less resource-hungry alternative.

    Every chat is assigned a unique ID and a flat file stored for it. Every chat adds a line to this file. Each client machine then uses jquery to check ONLY the modified date of the file, to see if the chat has been updated.

    While I would never normally recommend flat files over a database, I have a sneaky feeling that checking the modified date on a flat file would scale up better than the MySQL alternative.

    I was intrigued so I did some tests and here are the results:


    With an existing db connection, the number of "SELECT field FROM table LIMIT 0,1" that could be run in 1 second: ~ 4,000
    Opening and closing a db connection, but running the same query: ~ 1,800
    Checking the modified date on various different files: ~225,000


    So to check if a conversation has been updated, storing the conversations in flat files and checking for the last modified date would easily be faster than doing anything with a database.

    ReplyDelete
  6. In general, http connections are not very useful when it comes to pushing data to the client. Doing polls at every x seconds tend to be a resource hog on any server, given you have significant traffic.

    You should try XMPP combined with BOSH. Luckily, most of the heavy work is already done for you. You can implement a pure jquery (or other js framework) based solution very quickly. Read this tutorial, it will help you a lot - not only solving your specific problem but, giving you a broader view on how to implement push technologies over the good ole' http.

    ReplyDelete
  7. Unless, its a small-audience script - Between Database vs File-System, its better to use Database(.)

    P.S:- Flash also makes a great platform for chat servers, you might wanna look into that aswell.

    ReplyDelete
  8. If you define a conversation as only two people, then a request every second is going to look like one read request per second per user, and one write request every time somebody writes something (say every 10 seconds). So every 10 seconds you will have about 2.2 requests per second, per conversation.

    For 50 conversations, that's 100 users and 220 requests per second. That's a lot of load on a server for such a small number of conversations. Writing the conversation to JSON or XML, would probably provide a more scalable solution.

    This article discusses the architecture of Meebo - long-polling, comet.

    As an afterthought, have you considered installing an IM server like Jabber rather than starting from scratch?

    ReplyDelete
  9. you could always get the right tool for the job ... an XMPP compliant bit of software. for as poor as the documentation is, ejabber is pretty alright. because it follows closely the XMPP standard: http://code.google.com/p/ijab/ you can use any XMPP client. You can store all of it in an RDBMS if you like and provide similar functionalities that are offered in gmail / google talk.

    $0.02

    ReplyDelete
  10. I don't use it but you maybe can try Photon , a very high speed framework based on Mongrel.
    On the author blog (in french) you have a example , 30 lines of code for a real time chat server, with video demonstration.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.