Update presence member status causing presence member list to display updated user as only member

181 Views Asked by At

In Ably, Im using Ably React hook

In the documentation, there is a way to update member status when monitoring what users have entered a room.

const [presenceData, updateStatus] = usePresence("your-channel-name", "initial state");

// The `updateStatus` function can be used to update the presence data for the current client
updateStatus("new status");

This is where I'm having an issue. I take the presence Data and generate a list of users like this.

  const [presenceData, updateStatus] = usePresence(
    channel,
    {
      id: user.id,
      name: user.name,
      isModerator: false,
    },
    (presenceUpdate) => {
      presenceUpdate.action === "update" && console.log({ presenceUpdate });
    }

So my list generates correctly. In the callback, I see that the data(presenceUpdate) is updated correctly. When a user enters the room they are displayed correctly. When I start updating the status (ie: change the isModerator flag to true), the presenceData list shows the incorrect user data. It shows the updated user twice (assuming two users are in the room).

updateStatus({...user, isModerator: true})

When using channeling with presenceMember data together, updateStatus has weird side effects.

const [channel] = useChannel("your-channel-name", (message) => {
    console.log(message);
});

When you use updateStatus, are you just supposed to pass the updated member data or is there something else you need to do so your presenceMember data shows the correct information?

What Im expecting:

presenceData = [{id: 1, name: 'John', isModerator:false},{id:2, name: 'Steve', isModerator: true}]

What Im getting when I try updating Steve by setting isModerator to true:

presenceData = [{id:2, name: 'Steve', isModerator: true},{id:2, name: 'Steve', isModerator: true}]

Any ideas on what I'm doing wrong?

1

There are 1 best solutions below

0
On BEST ANSWER

I figured it out. You cant update anyone else's member data besides your own. I assumed I can have one user update the status of other users but what ends up happening is everyone's status is updated with the object in updateStatus(object). To make sure you dont update other users' data property, have some check that lets you decide if you are the user that is updating your status. This way, the updateStatus function is only called for that user and not for everyone else.