Jump to content

Recommended Posts

Posted

Hello,

I'm struggling with hooks to try and do this : adding user page to a page field when visiting a particular page on my website. In fact, I want to have a list of connected users on a particular page (called meeting-hall).

I've tried all kinds of things (which I don't really understand, sorry) but the latest is this :

	  $wire->addHookBefore('Page::render', function($e) {
    $page = $e->arguments(0);
    if ($page->is("name=meeting-hall")) {
      $page->connected->add($user); // connected is a Page field
      $page->save();
    } else {
      $meeting = wire("pages")->get("name=meeting-hall");
      $meeting->connected->remove($user);
      $meeting->save();
    }
  });
	

I put this in my _init.php file but nothing works...

The best I managed was updating my 'connected' field when the user loads the 'meeting hall' page by having this on the page template :

    if ($user->isLoggedin()) {
      $page->connected->add($user);
      $page->of(false);
      $page->save();
      $page->of(true);
    }

But I wanted to remove the user when he or she leaves the page... hence my thought about using hooks...

If you can give me a hint, I'd greatly appreciate !

Posted

Hi @bernhard,

Thanks for your interest.

Actually, it is not a big thing. I have a collaborative pad that I share with my middle-school students that resides on the Meeting Hall page. I have used the database session manager module to get a liist of logged-in users so that when someone is on the homepage, he or she can see if others are connected at the same time. And now, I would like to simply add a 'bell' next to the connected names when they actually are ON the Meeting Hall page so others can know they are there.

So my idea was to add a Page field on my meeting-hall template with connected users updating... as they come and go... Maybe that's a lot of 'fuss' for not so much, though...

 

Posted

Thx, that gives us a good picture. I'm just asking because determining the "online" state like this is not really reliable. What if the user opens another page on the website in a second tab? This would remove the online state on the meeting-hall but this window could still be open. Not sure how critical this online state is and I don't want to overcomplicate things. Just want to make sure you have thought of that ? 

An easy solution could be to save the timestamp of the last activity of the user on that meeting-hall page. Then you could show "last active 5min ago" and if that time goes beyond lets say 10min you show "offline" or hide the user from that list.

Maybe this would also make your setup easier, because you could simply add the timestamp to the users template and populate that whenever a user interacts with the meeting-hall page. A list of all active users would then be $pages->find("template=user, meetinghalltimestamp>$nowMinusTenMinutes")

 

  • Like 1
  • Thanks 1
Posted

Wouah. Thanks a lot for your explanations !

I had thought of the tabs issue, but couldn't think of another possibility ? And your timestamp idea sounds great !

I'll go ahead and try and do that instead, but it'll take ME some time so that's for tomorrow I guess ! But the more I think about it, the more I see how this can be made YOUR way. Thanks !

Posted

The problem is less so multiple tabs and more so spotty internet and detecting people leaving. The server cannot know if people disconnected due to whatever reason unless being constantly in communication with them(e.g. a heartbeat msg) and using timeouts to mark someone as offline once responses are late or missing. This get‘s even trickier with multiple servers as people could have connection to one of the servers but not the others. So someone is only offline of there‘s no connection to non of the servers. I‘m not sure if this is a problem for your usecase, but that‘s what‘s tricky about even just basic availability lists of users. There are solutions for such problems, but personally I‘d not even go with php to solve them, so it‘s nothing pw can help you with greatly.

Posted

Thanks @LostKobrakai. I understand the problem and my concern was really a very basic thing, even if some use cases 'pass through'. It is nothing professional ?  and @bernhard's solution seems to the point for me : if another user loads any page, he or she will get an indicator of how many connected users are online and for those being active on the Meeting Hall, let's say within the last 10 minutes, a 'bell' will be added to show this recent activity.

My only left concern now is when people are on the Meeting Hall page for more than 10 minutes, using the collaborative pad (which is embedded from another server), hence having no 'activity' on the page. I should need to update the timestamp. I'll have to see if it's possible to have a 'cron' function while connected on a particular page  or simply add a button on which a user can click to update his or her timestamp to say "I'm here !"...

Again, much work (for me) for not so much effect... ?

But thanks a lot for your help !

Posted
3 minutes ago, celfred said:

My only left concern now is when people are on the Meeting Hall page for more than 10 minutes, using the collaborative pad (which is embedded from another server), hence having no 'activity' on the page. I should need to update the timestamp. I'll have to see if it's possible to have a 'cron' function while connected on a particular page  or simply add a button on which a user can click to update his or her timestamp to say "I'm here !"...

Just add some javascript that polls the page in the background every 5 minutes or so: https://stackoverflow.com/a/8682723/6370411

Posted

Well, I'm getting there... almost !

I used JS to update timestamp. Everything works as expected, except... (sorry...) : only admin can list users ? The aforementioned code by @bernhard :

	$pages->find("template=user, meetinghalltimestamp>$nowMinusTenMinutes")
	

only returns result when I'm logged in as admin.

Should I set my timestamp field on another template or is there a way to 'open' this and is it a recommended practice ?

EDIT : I saw that adding 'include=all' to my request made it.

Thanks for all your help !

Posted
2 hours ago, celfred said:

I understand the problem and my concern was really a very basic thing, even if some use cases 'pass through'.

I can fully understand that. I just wanted to give the full picture. How much of it matters to your case is nothing I can decide ?

  • Thanks 1
Posted

Thanks for the links.  I haven't looked into sockets in quite some time, but I do have an upcoming project to use this with. Thank you!

  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...