<p>In the project I am currently working on, we have the need to develop a web chat application, not a very complex chat, just a way to connect two people to talk about a very specific topic, we don't need any kind of authentication for one of the two users, we don't have to support emoticons, avatars, or stuff like that.</p>

<p>Some project members suggested that we could use XMPP through BOSH, I said that is like trying to catch a fish with a boat's net, and proposed a simpler method, like a simple Ajax/MySQL web chat, but we're worried about the performance hit in the server because of the constant polling of many chats open at the same time.</p>

<p>Has anyone done something like this before? What would you recommend? </p>

[{'answer_id': 35509, 'author': 'John Millikin', 'author_id': 3560, 'author_profile': 'https://Stackoverflow.com/users/3560', 'pm_score': 3, 'selected': False, 'text': "<blockquote>\n  <p>What would you recommend? </p>\n</blockquote>\n\n<p>XMPP through BOSH</p>\n\n<p>There's no need to invent your own message format and transport protocol when somebody else has. If you try, it'll slowly grow to be just as complex as BOSH but without the benefit of third-party library support or standardization.</p>\n"}, {'answer_id': 35513, 'author': 'Ryan Guest', 'author_id': 1811, 'author_profile': 'https://Stackoverflow.com/users/1811', 'pm_score': 4, 'selected': True, 'text': '<p>You might also want to look into <a href="http://en.wikipedia.org/wiki/Comet_(programming)" rel="noreferrer">Comet</a>.</p>\n\n<p>It\'s used by GTalk, Meebo, and many <a href="http://ajaxian.com/archives/comet-a-new-approach-to-ajax-applications" rel="noreferrer">other chat applications</a>. A few years ago when I was experimenting with it, there weren\'t very many libraries or details about server architecture to implement it, but it looks like there is a lot more stuff out now.</p>\n\n<p>Have a look at the <a href="http://cometdproject.dojotoolkit.org/" rel="noreferrer">cometd</a> project for more technical information.</p>\n'}, {'answer_id': 35571, 'author': 'hoyhoy', 'author_id': 3499, 'author_profile': 'https://Stackoverflow.com/users/3499', 'pm_score': 0, 'selected': False, 'text': '<p>I thought everyone used cometd for this sort of thing. </p>\n'}, {'answer_id': 35586, 'author': 'John Millikin', 'author_id': 3560, 'author_profile': 'https://Stackoverflow.com/users/3560', 'pm_score': 2, 'selected': False, 'text': '<blockquote>\n<p>You might also want to look into Comet.</p>\n<p>I thought everyone used cometd for this sort of thing.</p>\n</blockquote>\n<p>BOSH is a standard for transporting XMPP over HTTP. It involves Comet for pushing data to the client.</p>\n'}, {'answer_id': 35587, 'author': 'Toran Billups', 'author_id': 2701, 'author_profile': 'https://Stackoverflow.com/users/2701', 'pm_score': 1, 'selected': False, 'text': '<p>I did this very same thing a few months back and had fun just playing around with the concepts.  I actually used the <a href="http://cometdaily.com/2007/11/05/the-forever-frame-technique/" rel="nofollow noreferrer">forever-frame</a> technique instead of polling.</p>\n\n<p>The below code is my "comet" js file that contains the general concepts required to get a "party chat" setup.</p>\n\n<pre><code>function Comet(key) {\n\n  var random = key;\n  var title = \'Comet\';\n  var connection = false;\n  var iframediv = false;\n  var browserIsIE = /*@cc_on!@*/false;\n  var blurStatus = false;\n  var tmpframe = document.createElement(\'iframe\');\n  var nl = \'\\r\\n\';\n\n  this.initialize = function() {\n    if (browserIsIE) {\n      connection = new ActiveXObject("htmlfile");\n      connection.open();\n      connection.write("&lt;html&gt;");\n      connection.write("&lt;script&gt;document.domain = \'"+document.domain+"\'");\n      connection.write("&lt;/html&gt;");\n      connection.close();\n      iframediv = connection.createElement("div");\n      connection.appendChild(iframediv);\n      connection.parentWindow.comet = comet;\n      iframediv.innerHTML = "&lt;iframe id=\'comet_iframe\' src=\'./comet.aspx?key="+random+"\'&gt;&lt;/iframe&gt;";\n    } else {\n      connection = document.createElement(\'iframe\');\n      connection.setAttribute(\'id\', \'comet_iframe\');\n      iframediv = document.createElement(\'iframe\');\n      iframediv.setAttribute(\'src\', \'./comet.aspx?key=\'+random);\n      connection.appendChild(iframediv);\n      document.body.appendChild(connection);\n    }\n  }\n\n  // this function is called from the server to keep the connection alive\n  this.keepAlive = function () {\n    if (!browserIsIE) {\n        mozillaHack();\n    }\n  }\n\n  // this function is called from the server to update the client\n  this.updateClient = function (value) {\n    var outputDiv = document.getElementById(\'output\');\n    outputDiv.value = value + nl + outputDiv.value;\n    if (blurStatus == true) {\n        document.title = value;\n    }\n    if (!browserIsIE) {\n        mozillaHack();\n    }\n  }\n\n  this.onUnload = function() {\n    if (connection) {\n      // this will release the iframe to prevent problems with IE when reloading the page\n      connection = false;\n    }\n  }\n\n  this.toggleBlurStatus = function(bool) {\n    blurStatus = bool;\n  }\n\n  this.resetTitle = function() {\n    document.title = title;\n  }\n\n  function mozillaHack() {\n    // this hack will fix the hour glass and loading status for Mozilla browsers\n    document.body.appendChild(tmpframe);\n    document.body.removeChild(tmpframe);\n  }\n}\n</code></pre>\n'}, {'answer_id': 35626, 'author': 'UnkwnTech', 'author_id': 115, 'author_profile': 'https://Stackoverflow.com/users/115', 'pm_score': 0, 'selected': False, 'text': "<p>I agree with John.  But there was another question that was not answered.<br />\nI have done this but instead of using a database we used a flat file, it did eventually cripple the server, but it wasn't until we has ~450 active users, and if we had done it with a database it probably would have fared better.<br />This was done on a basic hosting account from Godaddy.</p>\n\n<p>Edit: BTW Godaddy sounded less then amused when I got the phone call.</p>\n"}, {'answer_id': 111125, 'author': 'Jimmy', 'author_id': 3586, 'author_profile': 'https://Stackoverflow.com/users/3586', 'pm_score': 0, 'selected': False, 'text': '<p>I think polling is the simplest approach and would recommend that first. If the load becomes a problem start, looking into more complicated techniques.\nA good discussion on the pros and cons are here - \n<a href="http://www.infoq.com/news/2007/07/pushvspull" rel="nofollow noreferrer">http://www.infoq.com/news/2007/07/pushvspull</a><br>\n<a href="http://ajaxian.com/archives/a-report-on-push-versus-pull" rel="nofollow noreferrer">http://ajaxian.com/archives/a-report-on-push-versus-pull</a></p>\n'}, {'answer_id': 111130, 'author': 'zuber', 'author_id': 9812, 'author_profile': 'https://Stackoverflow.com/users/9812', 'pm_score': 2, 'selected': False, 'text': '<p>There is a very good server for handling message pushing from server to browser (dubbed <a href="http://en.wikipedia.org/wiki/Comet_(programming)" rel="nofollow noreferrer">Comet</a>) - <a href="http://orbited.org/" rel="nofollow noreferrer">Orbited</a>. It\'s easily integrated with other technologies (Django, Rails, PHP etc.) just like memcached.</p>\n\n<p>You really should check it if you want to handle serious load. Otherwise, simple Ajax polling is the best way.</p>\n'}, {'answer_id': 111139, 'author': 'moonshadow', 'author_id': 11834, 'author_profile': 'https://Stackoverflow.com/users/11834', 'pm_score': 1, 'selected': False, 'text': '<p>The trick is to realise that the only time your app needs to invoke CGI on the server is when someone says something. For the regular polls, poll a static page that your CGI script updates whenever there is new chat. Use HEAD requests, compare the timestamps with those last seen, and only do a full GET when those change. I have a simple naive chat application implemented this way, and the load and bandwidth usage is negligible for the few tens of simultaneous users we have.</p>\n'}, {'answer_id': 111315, 'author': 'Svante Svenson', 'author_id': 19707, 'author_profile': 'https://Stackoverflow.com/users/19707', 'pm_score': 2, 'selected': False, 'text': "<p>If you don't like the idea of HTTP-polling, you could have a Flash-movie on the chat page that has a constant connection to some deamon on the server, the Flash-movie would then invoke JavaScript functions on the client to update the chat as new messages comes along. (Unless you want a Flash interface for your chat..)</p>\n"}, {'answer_id': 829066, 'author': 'melo', 'author_id': 24579, 'author_profile': 'https://Stackoverflow.com/users/24579', 'pm_score': 0, 'selected': False, 'text': '<p>Checkout <a href="http://www.speeqe.com/" rel="nofollow noreferrer">Speeqe</a>. Its a <a href="http://code.stanziq.com/speeqe/" rel="nofollow noreferrer">open-source solution</a> for Web-based chat rooms that uses BOSH and XMPP behind the scenes.</p>\n'}, {'answer_id': 12430556, 'author': 'Alain Tiemblo', 'author_id': 731138, 'author_profile': 'https://Stackoverflow.com/users/731138', 'pm_score': 0, 'selected': False, 'text': '<p>I just found this post, it is old, but polling concept gives troubles for a lot of poeple. So i\'ll put an implementation example here. But before giving it to you, I should give you an advice that made me mad some time ago :</p>\n\n<p>When you poll, you should take care of sessions behaviour (<a href="http://thwartedefforts.org/2006/11/11/race-conditions-with-ajax-and-php-sessions/" rel="nofollow">race conditions</a>). To make it simple : if you open a session, the session file is locked until the session is closed to avoid 2 theads writting different data into it. So, if you need a session to check if a user is logged or so, always close the session before polling.</p>\n\n<p>My demo gives you an example of a polling implementation in PHP. I will not use a database, but a file instead. When you click polling button, you will enter the loop and until the file is modified, you will stay polling. When you fill the form and click Release, what you typed will be saved into the file. Modification time of the file will change so the polling will stop.</p>\n\n<p>Tip: use a tool like <a href="https://getfirebug.com/" rel="nofollow">Firebug</a> to see what\'s happen.</p>\n\n<p>Now lets speak in a better langage than my english :</p>\n\n<pre><code>&lt;?php\n\n    // For this demo\n    if (file_exists(\'poll.txt\') == false) {\n        file_put_contents(\'poll.txt\', \'\');\n    }\n\n    if (isset($_GET[\'poll\'])) {\n\n        // Don\'t forget to change the default time limit\n        set_time_limit(120);\n\n        date_default_timezone_set(\'Europe/Paris\');\n        $time = time();\n\n        // We loop until you click on the "release" button...\n        $poll = true;\n        $number_of_tries = 1;\n        while ($poll)\n        {\n            // Here we simulate a request (last mtime of file could be a creation/update_date field on a base)\n            clearstatcache();\n            $mtime = filemtime(\'poll.txt\');\n\n            if ($mtime &gt; $time) {\n                $result = htmlentities(file_get_contents(\'poll.txt\'));\n                $poll = false;\n            }\n\n            // Of course, else your polling will kill your resources!\n            $number_of_tries++;\n            sleep(1);\n        }\n\n        // Outputs result\n        echo "Number of tries : {$number_of_tries}&lt;br/&gt;{$result}";\n        die();\n    }\n\n    // Here we catch the release form\n    if (isset($_GET[\'release\']))\n    {\n        $data = \'\';\n        if (isset($_GET[\'data\'])) {\n            $data = $_GET[\'data\'];\n        }\n        file_put_contents(\'poll.txt\', $data);\n        die();\n    }\n\n?&gt;\n\n&lt;!-- click this button to begin long-polling --&gt;\n&lt;input id="poll" type="button" value="Click me to start polling" /&gt;\n\n&lt;br/&gt;&lt;br/&gt;\n\nGive me some text here :\n&lt;br/&gt;\n&lt;input id="data" type="text" /&gt;\n&lt;br/&gt;\n\n&lt;!-- click this button to release long-polling --&gt;\n&lt;input id="release" type="button" value="Click me to release polling" disabled="disabled" /&gt;\n\n&lt;br/&gt;&lt;br/&gt;\n\nResult after releasing polling :\n&lt;div id="result"&gt;&lt;/div&gt;\n\n&lt;script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"&gt;&lt;/script&gt;\n&lt;script type="text/javascript"&gt;\n\n// Script to launch polling\n$(\'#poll\').click(function() {\n    $(\'#poll\').attr(\'disabled\', \'disabled\');\n    $(\'#release\').removeAttr(\'disabled\');\n    $.ajax({\n        url: \'poll.php\',\n        data: {\n            poll: \'yes\' // sets our $_GET[\'poll\']\n        },\n        success: function(data) {\n            $(\'#result\').html(data);\n            $(\'#poll\').removeAttr(\'disabled\');\n            $(\'#release\').attr(\'disabled\', \'disabled\');\n        }\n    });\n});\n\n// Script to release polling\n$(\'#release\').click(function() {\n    $.ajax({\n        url: \'poll.php\',\n        data: {\n            release: \'yes\', // sets our $_GET[\'release\']\n            data: $(\'#data\').val() // sets our $_GET[\'data\']\n        }\n    });\n});\n\n&lt;/script&gt;\n</code></pre>\n\n<h1>You can try it <a href="http://home.fuz.org/tests/poll.php" rel="nofollow">here</a></h1>\n'}]
['https://Stackoverflow.com/questions/35499', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3695/']