Skip to content

Instantly share code, notes, and snippets.

@thisisjofrank
Created April 22, 2022 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thisisjofrank/bc0415c85c2bcb1c376447fb58b6cc59 to your computer and use it in GitHub Desktop.
Save thisisjofrank/bc0415c85c2bcb1c376447fb58b6cc59 to your computer and use it in GitHub Desktop.
If there is no leader, we sort the members array alphabetically, using the clientId property of each PresenceMessage:
...
const sortedMembers = members.sort(
(a, b) => (a.connectionId as any) - (b.connectionId as any)
);
if (sortedMembers[0].clientId === this.id) {
await this.channel.presence.update("leader");
this.onElection(this.channel);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment