Hello,

I want to add to my front page chat button how many users are currently in a chatroom.

Here's the code that I have:

  foreach (chatroom_get_site_online_list($_SESSION['drupal_user']->uid) as $user) {
  	$num_online++;
  	$users_online[] = $user->name;
  }
  if ($num_online > 0) {
  	$users_online = implode(', ', $users_online);
  }
  echo "<div>$num_online online: $users_online</div>";

When I first enter a chatroom and check the front page, everything seems to work, but after a few seconds of inactivity the user list becomes empty.

I understand that the updates are made based on an "event" such as text submit or user join/leave. Is there a way where I can trigger such an event from php on my front page, so I get a real-time check of who's actually online?

Thanks,
Andrey.

Comments

darren oh’s picture

Status: Active » Closed (fixed)
mr.andrey’s picture

Status: Closed (fixed) » Active

Hi Darren Oh,

Thanks for the reply.

I looked at the chatroom_block_online_list_update() function and plugged it into my front page like this:

  $uid = $_SESSION['drupal_user']->uid;

  chatroom_site_update_online_time($uid);
  $users = chatroom_get_site_online_list($uid);
  $js = array();
  if (!empty($users)) {
    foreach ($users as $user) {
      $js[] = array(
        'user' => $user->name,
        'uid' => $user->uid,
      );
    }
  }

  var_export($js); // print the output

There are two things that are happening.

* If I enter a chatroom as one user, stay idle for 5+ seconds and look at the front page with a second user, the first user no longer appears on the list.

* This doesn't actually return the number of users in chat - if I come to the front page as the first user, then back with a second, I will see the first user in the list even though that user never entered chat.

Any ideas?

Thanks,
Andrey.

darren oh’s picture

In that case you should look at chatroom_chat_write_msg().

mr.andrey’s picture

Which part of this function triggers the "event"? Is it javascript or php, or both? I'm not quite sure what to look for here.

I'm looking for a way to trigger the "event" before calling on chatroom_get_site_online_list() function, so that the list of the users who are logged in that chat is current.

Thanks,
Andrey.

function chatroom_chat_write_msg($chat_id, $last_msg_id, $update_count, $cache_file, $msg, $recipient = NULL, $type = NULL, $timezone = 0, $smileys = FALSE) {
  global $user;
  $guest_id = chatroom_chat_is_registered_user($chat_id);
  if ($guest_id === FALSE) {
    $js->chatUsers = array();
  }
  else {
    if ($type == 'away' || $type == 'back') {
      db_query("
        UPDATE {chatroom_online_list} SET away = %d WHERE ccid = %d AND session_id = '%s'
      ", $type == 'away', $chat_id, session_id());
      chatroom_chat_update_online_time($chat_id);
    }
    else {
      db_query("
        UPDATE {chatroom_online_list} SET away = 0 WHERE ccid = %d AND session_id = '%s'
      ", $chat_id, session_id());
      db_query("
        INSERT INTO {chatroom_msg}
        (ccid, uid, msg_type, msg, session_id, recipient, modified)
        VALUES (%d, %d, '%s', '%s', '%s', '%s', %d)
      ", $chat_id, $user->uid, $type, $msg, session_id(), $recipient, time());
    }
    $js->msgs = chatroom_chat_get_latest_msgs($chat_id, $user->uid, $last_msg_id, $timezone, $smileys);
    $cache_timestamp  = @filemtime($cache_file);
    $js->chatUsers = chatroom_chat_get_online_list($chat_id, $cache_timestamp, $update_count);
    $cache_timestamp = chatroom_chat_update_cache($cache_file);
    if (!empty($cache_timestamp)) {
      $js->cacheTimestamp = $cache_timestamp;
    }
  }
  return chatroom_send_ajax_response(drupal_to_js($js));
}
darren oh’s picture

chatroom_chat_update_cache() triggers the event. You will need the chat ID to make it work:
chatroom_chat_update_cache(_chatroom_get_cache_file("chat.$chat_id"));

mr.andrey’s picture

Thanks, Darren Oh. This is exactly what I was looking for.

I'm running into a small problem though. Most of the times it works fine, but if I leave the browser alone for 10 seconds, then come back to refresh the front page, no users show up. If I refresh again, and keep refreshing, they show up. This is fairly consistent. It seems to be all or nothing - either all online chat users show up or none at all.

Here's the code that I have:

  chatroom_chat_update_cache(_chatroom_get_cache_file("chat.1"));
  $online_list = chatroom_get_site_online_list('0');
  foreach ($online_list as $user) {
      $online[] = "<a href='/user/".$user->uid."'>".$user->name."</a>"; 
  }
  $online_users = implode(', ', $online);
  echo $online_users;

Any ideas what might be happening?

UPDATE:
Actually, this shows all users who are online who are not even in chat. It seems to be showing those that have been active in the last five seconds.

Thanks,
Andrey.

pomliane’s picture

Status: Active » Closed (won't fix)

This version of Chat Room is not supported anymore. The issue is closed for this reason.
Please upgrade to a supported version and feel free to reopen the issue on the new version if applicable.

This issue has been automagically closed by a script.