Manage User Presence

Collaboration Engine 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.setNewUserHandler(newUserInfo -> { 3
    Component card = createUserCard(newUserInfo);
    users.add(card);
    return () -> users.remove(card); 4
});
  1. 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.

  2. 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.

  3. The setNewUserHandler method sets a callback to handle a new user joining the topic. It gets the UserInfo for the new user as the parameter.

  4. The callback returns another callback, a registration, that is called to remove the same user when the user leaves the topic.