Frage

When a user creates a room, is that user given some sort of identifier that can be used to give them special/separate functions within the room?

I'm thinking specifically of a follow-the-leader-type scenario, where the room creator is the only one who can affect the page.

(I presume this is easily done via formal user authentication, but I'm curious as to what happens out of the box.)

War es hilfreich?

Lösung

There is currently no way to identify which user joined a room first. Currently, the only way to do this is to use a JWT that identifies which users are leaders or followers by their groups.

You can then leverage our ACL's to ensure that only the leader can modify certain data. For retrieving the users groups you can call get on the relevant user key for displaying the appropriate views inside your application.

On our roadmap is the ability to read group and user information from a key in the ACL meaning that when a user joins a room they could perform a set with overwrite false. If no other user had set the key they would automatically be a member of the group.

If you're not worried about proper user authentication, you could determine the user by leveraging set overwrite. The following is an example:

var leaderKey = room.key('leader');
function acquireLeader (cb) {
  leaderKey.set(user.id, { overwrite: false, cascade: room.self() }, function (err) {
    if (err instanceof goinstant.errors.CollisionError) {
      return cb(null, false); // someone else is the leader
    } else if (err) {
      return cb(err); // something else went wrong
    }

    cb(null, true);
  });
}

room.join(function(err) {
  if (err) {
    throw err;
  }

  acquireLeader(function(err, isLeader) {
    if (err) {
      throw err;
    }

    console.log('are you the leader?', isLeader);


    leaderKey.on('remove', function (value, context) {
      acquireLeader(function(err, isLeader) {
        console.log('did you acquire leader?', isLeader);
      });
    });
  });
});

The above leverages set overwrite and key cascading to ensure when a user joins the room they attempt to become the leader. If they key does not have a value their user id is stored in the leader key. If the key already has a value then someone else is the leader.

When the user leaves the room, the leader key will be removed and then leadership-election can occur again. Meaning someone will always be the leader!

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top