Edward Posted April 10, 2009 Share Posted April 10, 2009 Hi, I'm creating a one-to-one PHP / MySQL chat feature for my website, so people can chat to me when they visit. I'm using AJAX to try and make this instant, but at the moment I can only make the messages refresh on an interval, i.e. every five seconds. What I want to do is try and make it instant, so that as soon as I send a message to someone, they receive it. I guess I only want it to refresh when there's something new to display. The problem is I don't know how to detect this on the visitor's end. I thought I could loop an SQL command until a message was found with the status 'unread', but looping a SELECT SQL statement seems to cause it to freeze, though it's possible I was doing it wrong. Here is what I am using so far, which refreshes on an interval. Index file: <script type="text/javascript" src="chat_event_view_ajax.js"></script> <script type="text/javascript" src="chat_event_add_ajax.js"></script> <?php include('chat_form.php'); ?> AJAX for Submitting a form, triggering function chat_event_add(): var xmlHttp_chat_event_add; function GetXmlHttpObject_chat_event_add() { var xmlHttp_chat_event_add=null; try { // Firefox, Opera 8.0+, Safari xmlHttp_chat_event_add=new XMLHttpRequest(); } catch (e) { // Internet Explorer try { xmlHttp_chat_event_add=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xmlHttp_chat_event_add=new ActiveXObject("Microsoft.XMLHTTP"); } } return xmlHttp_chat_event_add; } function stateChanged_chat_event_add() { if (xmlHttp_chat_event_add.readyState==4) { document.getElementById("thread").innerHTML=xmlHttp_chat_event_add.responseText; $(function() { $("#thread-container").scrollTo(10000000, 500); }); // TRIGGER THE 'VIEW' AJAX FUNCTION interval_chat_event_view = setInterval('chat_event_view()', 5*1000); // milliseconds between requests } } function chat_event_add() { xmlHttp_chat_event_add=GetXmlHttpObject_chat_event_add(); if (xmlHttp_chat_event_add==null) { alert ("Your browser does not support AJAX!"); return; } var url_chat_event_add="chat_event_add_sql.php"; url_chat_event_add=url_chat_event_add+"?form_chat_from=" + document.getElementById("form_chat_from").value; url_chat_event_add=url_chat_event_add+"&form_chat_alias=" + document.getElementById("form_chat_alias").value; url_chat_event_add=url_chat_event_add+"&form_chat_message=" + document.getElementById("form_chat_message").value; url_chat_event_add=url_chat_event_add+"&rand="+Math.random(); xmlHttp_chat_event_add.onreadystatechange=stateChanged_chat_event_add; xmlHttp_chat_event_add.open("GET",url_chat_event_add,true); xmlHttp_chat_event_add.send(null); // Empty the message input field document.getElementById("form_chat_message").value = ''; } The PHP file, chat_event_add_sql.php, adds a message to the database. The AJAX file, for chat_event_view(), is similar to the above, but links to chat_event_view(), which displays the messages from the database. I hope I have included enough information, so that someone can help me make this instant. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/153460-solved-trying-to-make-phpmysql-instant-messaging-instant/ Share on other sites More sharing options...
xtopolis Posted April 11, 2009 Share Posted April 11, 2009 What you want is a push type chat system, what you have is a polling system. There isn't a push type method available for PHP (there are some, but they are apache mods afaik). Looping through mysql until an unread message is found is not smart as the condition may never be reached... on top of that there are script timeouts, mysql timeouts(?), etc to worry about. Google AJAX chat system.. there are many out there that you can learn from. Quote Link to comment https://forums.phpfreaks.com/topic/153460-solved-trying-to-make-phpmysql-instant-messaging-instant/#findComment-806980 Share on other sites More sharing options...
Edward Posted April 14, 2009 Author Share Posted April 14, 2009 Thanks xtopolis, a push system is exactly what I want, I didn't know that's what it was called. I guess I'll keep looking, though understand it's not possible yet with PHP. I have it working pretty well with AJAX so far. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/153460-solved-trying-to-make-phpmysql-instant-messaging-instant/#findComment-810123 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.