Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Buddypress Unread message count - live update?

I am using the following code to display the logged in users unread message count in a template.

  <?php if (messages_get_unread_count() != 0) { ?>

 <div id="message_count">
         <div class="countspan">
    <?php echo messages_get_unread_count(); ?>
         </div>
     </div>
   <?php } ?>

This works great however it requires a page refresh to update. Since I am making heavy use of ajax driven navigation throughout the site and the custom apps within it this is not a satisfactory solution.

How can make the counter update automatically?

I have played around with the 'lvive notifications' plugin which polls the srver every 10 seconds to provide live notifications but of course this does not interact with my custom unread message counter.

Any ideas guys? I could really use the help.

like image 822
gordyr Avatar asked Feb 22 '23 21:02

gordyr


2 Answers

I've worked it out...

It turns out buddypress has built in ajax functions for a lot of this stuff...

So for anyone else wanting to do the same thing.... It is a simple case of putting the 'count' inside an a link with the following id.

  <a id="user-messages">

<span><?php echo messages_get_unread_count(); ?></span>
</a>

Buddypress' javascript then does the rest for you. Simple!

Mana thanks for your suggestion though.

like image 125
gordyr Avatar answered May 14 '23 15:05

gordyr


You could use setTimeout to periodically fire a request to the server which invokes your messages_get_unread_count() and returns a value.

Then based on the value returned, you could show or hide your <div id="message_count"> with the updated count.

like image 34
Pat Avatar answered May 14 '23 13:05

Pat