Handle User Presence with Your Own Logic
Collaboration Kit includes a manager to set the user presence in a topic, keep track of the users who are currently present, and react to changes in the presence. It provides a simple and flexible way to manage topic data related to user presence and makes it easy to create custom components with collaborative features.
The following example has a custom component that shows the list of active users in a topic using the presence manager:
VerticalLayout users = new VerticalLayout();
UserInfo localUser = new UserInfo("john");
PresenceManager manager = new PresenceManager(users, localUser,
"my-topic"); // (1)
manager.markAsPresent(true); // (2)
manager.setPresenceHandler(context -> { // (3)
Component card = createUserCard(context.getUser());
users.add(card);
return () -> users.remove(card); // (4)
});
-
Creating a
PresenceManager
takes:-
a component to bind the manager connection context to the UI,
-
an instance of
UserInfo
that represents the current local user, and -
the ID of the topic to connect to.
-
-
The
markAsPresent()
method is used to configure the manager to mark the local user as present in the topic as soon as a connection is established. -
The
setPresenceHandler
method sets a callback to handle a new user joining the topic. TheUserInfo
for the new user is provided by the context passed as the argument to the callback. -
The callback returns another callback, a registration, that’s called to remove the same user when the user leaves the topic.
3087B264-5D09-43C6-A195-07FAFF288CA7