• Forum
  • Doc
  • Screenshots
  • Download
  • Donate
  • Contributors
  • Contact
  • Follow @phpfreechat
  • DEMO
  • Board index ‹ Version 1.x branch ‹ Contributions (v1.x)
  • Change font size
  • FAQ
  • Register
  • Login

Who\'s Online using AJAX to refresh the counter

Post a bug fix, a new feature, a theme ...

Moderators: OldWolf, re*s.t.a.r.s.*2

Post a reply
14 posts • Page 1 of 1

Postby JohnDoe » Wed May 17, 2006 5:05 pm

After a few hours of headache ive made a who's online script for the freeChat lib , however my code is very messy and needs putting into functions , if anyone would like to see / have the code they are more than welcome , just reply or send me an email ..

The who is online is in realtime using AJAX + PHP + MySQL

Thanks
and nice work
/John

Edit :
i should state why i created the script as reading your notes and so on it seems you wanted to steer away from MySql.
Well i started to think about a portal for my web site where people could get online realtime help , and had the brainwave of chatting .. exept without being invloved in the chats i would never know if someone was needing any help , so i made a kind of admin page where it would retrieve the users details from a database and the chan name they were in so i could join the chan to help them .... which has now gone into to being plugged into my user relation software on my intranet .. its all experimental @ the mo as im bored at work .. anyway updates as they happen
tata
Last edited by JohnDoe on Wed May 17, 2006 5:14 pm, edited 1 time in total.
JohnDoe
New member
 
Posts: 6
Joined: Wed May 17, 2006 5:00 pm
Top

Postby JohnDoe » Thu May 18, 2006 9:06 am

Code: Select all
<script language="javascript">
// The Ajax //
var  dataTimer = "";


function startLookup() {
StartTimer();
sendRequest();
}
function StartTimer() {
setInterval("sendRequest()", 1000);
}
    function createRequestObject() {

       var req;
   
       if(window.XMLHttpRequest){
          // Firefox, Safari, Opera...
          req = new XMLHttpRequest();
       } else if(window.ActiveXObject) {
          // Internet Explorer 5+
          req = new ActiveXObject("Microsoft.XMLHTTP");
       } else {
          // There is an error creating the object,
          // just as an old browser is being used.
          alert('Problem creating the XMLHttpRequest object');
       }
   
       return req;
   
    }
   
    // Make the XMLHttpRequest object
    var http = createRequestObject();
   
    function sendRequest(position) {
   
       // Open PHP script for requests
       http.open('get', 'whosonline.php');
       http.onreadystatechange = handleResponse;
       http.send(null);
        }
   
    function handleResponse() {
   
       if(http.readyState == 4 && http.status == 200){
   
          // Text returned FROM the PHP script
          var response = http.responseText;
   
          if(response) {
             // UPDATE ajaxTest content
             document.getElementById("searchResults").innerHTML = response;
          }
   
       }
   
    }

</script>

Below your chat container you need to put a div like so
Code: Select all
<body onLoad="startLookup();">
  <?php $chat->printChat(); ?>
<br />
<div id="searchResults"></div>
  </body>
 
</html>

now for the whosonline.php
Code: Select all
<?
/* THE PHP */
include('connex/config.php');
$conncheck = mysql_connect("$db_host", "$db_user", "$db_pass") or die("Failed to connect to mysql server");
mysql_select_db("chatHistory", $conncheck) or die("Could Not connect to the DATABASE");
$q = "SELECT * FROM chatHistory";
$result = mysql_query($q);
$totalOnline = mysql_num_rows($result);
echo "<b>$totalOnline</b> Users Online (Name , Chatroom , Chat Button)<br>";

for($i = 1; $i <= $totalOnline; $i++)
{
$i = $i++;


$row = mysql_fetch_array($result);


echo "
<table width="600px">
<tr>
<td width="50px">$i.</td>
<td width="150px">$row[name]</td>
<td width="200px">$row[room]</td>
<td width="150px"> <button class="applyButtonLong" onclick="document.location.href='';"><a href="">Chat To Them</a></button>
</td></tr>";

}
?>

Say something like that ... this is unfinished yet i am finishing it today (i did warn about messy code!!)
Last edited by JohnDoe on Thu May 18, 2006 9:27 am, edited 1 time in total.
JohnDoe
New member
 
Posts: 6
Joined: Wed May 17, 2006 5:00 pm
Top

Postby phpfreechat » Thu May 18, 2006 10:11 am

Thank you, this is clear now.

My first comment is about the mysql stuff.
How do you fill the mysql chatHistory table ? did you wrote a mysql container ?

ps: I edited the topic subject to me be more explicit. And I deleted the no-count replys.
phpfreechat
Site Admin
 
Posts: 2657
Joined: Tue Feb 07, 2006 3:35 pm
Location: France
Top

Postby JohnDoe » Thu May 18, 2006 10:19 am

sorry was a simple insert command on page open something like this
Code: Select all
<?
include('connex/config.php');
$conncheck = mysql_connect("$db_host", "$db_user", "$db_pass") or die("Failed to connect to mysql server");
mysql_select_db("chatHistory", $conncheck) or die("Could Not connect to the DATABASE");
mysql_query("INSERT into `chatHistory` (`name`,`ip`,`room`,`date`,`timeStamp`,`finished`,`sess_id`) VALUES ('$chatNickF $chatNickS','$ip','$chatRoom','$dateIns','$timeStamp','0','$sessId')");
?>

GThe trouble im having at the moment is , Internet explorer is caching the variables and not refreshing the AJAX ... but Firefox is working perfectly aside asking me for an input request after i have chosen a nick !!
DB table Structure:

Code: Select all

In database chatHistory (though this is left to choice) <-- for development purposes !!
JohnDoe
New member
 
Posts: 6
Joined: Wed May 17, 2006 5:00 pm
Top

Postby phpfreechat » Thu May 18, 2006 12:32 pm

Ok, it a bit more clear for me now :)

However, one more question:
How do you manage the quit action ? if a user connect and then quit the chat, do you remove the user nickname from the database ?
phpfreechat
Site Admin
 
Posts: 2657
Joined: Tue Feb 07, 2006 3:35 pm
Location: France
Top

Postby JohnDoe » Thu May 18, 2006 12:56 pm

This is the next bit for me to finish, im thinking of the best way to achieve it at the mo it will prolly result in the db row being moved into an archive state for logging purposes and reports, to the best of my knowledge (not alot) its hard to determine when someone closes browser window .. though someone might know a way
/John
JohnDoe
New member
 
Posts: 6
Joined: Wed May 17, 2006 5:00 pm
Top

Postby phpfreechat » Thu May 18, 2006 2:33 pm

The way I use to know if a user is disconnected or not in phpfreechat :
- a timeout parameter, I disconnect each users who didn't update since "current time - timeout value".

Your approch is a bit strange because you create a place where you must keep up to date the online users list. However in phpfreechat container this information is allready known by the container.
In fact, this information can be found with a hack in 0.X branche, look at these topics :
- http://www.phpfreechat.net/forum/viewtopic.php?id=126
- http://www.phpfreechat.net/forum/viewtopic.php?id=7
phpfreechat
Site Admin
 
Posts: 2657
Joined: Tue Feb 07, 2006 3:35 pm
Location: France
Top

Postby JohnDoe » Thu May 18, 2006 2:45 pm

ive found an Ajax / javascript way to catch the user when closing the browser window , so i'll make it popup an alert box and execute a php script somehow or set a variable 1 somewhere i'll find the info and post it here
/John
Last edited by JohnDoe on Thu May 18, 2006 2:46 pm, edited 1 time in total.
JohnDoe
New member
 
Posts: 6
Joined: Wed May 17, 2006 5:00 pm
Top

Postby Mania » Fri Mar 02, 2007 7:38 am

kerphi wrote:The way I use to know if a user is disconnected or not in phpfreechat :
- a timeout parameter, I disconnect each users who didn't update since "current time - timeout value".

Hey kerphi,

Would you mind telling how you disconnect the users in the way you mentioned?

I am currently trying to integrate phpfreechat to my site. I downloaded the newest 1.x (beta 9) and been confing it the past few days. It's been a pain in the ass to modify but still I think it is the best chat out there for my needs. :) I use the mysql container and I would like to count the current online users from the database. I noticed that using a query that counts the "leaf" when the subgroup is set to "nick" would make this possible. This seems to be fine for more than 1 users but after the last one leaves the chat the database still has data about this last person. So I would like to know where and how to put a script that disconnects the person after certain time and removes the data from the database.

Thanks.

Btw. I am using mysql container to make it better performance wise. Although after reading the forum I'm still not quite sure as to if the mysql is faster than using the flat files? Also I would like to know have the performance issues been enhanced since earlier versions where people complained that the chat is unusable for over 50 concurrent users?
Mania
Member
 
Posts: 11
Joined: Fri Mar 02, 2007 7:22 am
Top

Postby phpfreechat » Sun Mar 04, 2007 2:42 pm

getOnlineNick has two parameters, the second one is the timeout (in seconds).
phpfreechat
Site Admin
 
Posts: 2657
Joined: Tue Feb 07, 2006 3:35 pm
Location: France
Top

Postby Mania » Wed Mar 07, 2007 8:29 am

Umm.. sorry but I think I need a little more to go on this one. For instance where is this getOnlineNick function located? If you have a working solution available I wouldn't mind a little copy-pasting.. :)
Mania
Member
 
Posts: 11
Joined: Fri Mar 02, 2007 7:22 am
Top

Postby pinup57 » Wed Mar 07, 2007 9:14 am

I'm very interested in this "whoisonline" stuff too...... same problem, the chat being just a part of my site, not on the homepage, I'd like users who browse the webpages to be notified in realtime what's happening in the chat, otherwise noone will ever go there..... Has any progress been made?

Dirk
pinup57
Member
 
Posts: 89
Joined: Sat Feb 24, 2007 8:13 am
Top

Postby Mania » Thu Mar 08, 2007 8:35 am

I got JohnDoe's code working together with my whosonline.php that just prints the number of people in chat. This AJAX refresh is working perfectly in Firefox 2, but IE 6 & 7 only print the number of people once (when the browser loads the page the first time - not working after this even with a manual page refresh). Anyone confirm this code works properly in IE's as well?
Mania
Member
 
Posts: 11
Joined: Fri Mar 02, 2007 7:22 am
Top

Postby Mania » Thu Mar 08, 2007 9:23 am

I have it working now in all browsers, the problem was with IE caching issue. A post about it and fix can be found from http://ajaxian.com/archives/ajax-ie-caching-issue
Mania
Member
 
Posts: 11
Joined: Fri Mar 02, 2007 7:22 am
Top


Post a reply
14 posts • Page 1 of 1

Return to Contributions (v1.x)

Who is online

Users browsing this forum: No registered users and 10 guests

  • Board index
  • The team • Delete all board cookies • All times are UTC + 1 hour
Powered by phpBB® Forum Software © phpBB Group
Sign in
Wrong credentials
Sign up I forgot my password
.
jeu-gratuit.net | more partners
Fork me on GitHub