This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| swarm.connect(); | |
| export { }; // required for vite to import the file |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| swarm.onConnection = (channel: Types.RealtimeChannelPromise) => { | |
| channel.subscribe("message", (message: Types.Message) => { | |
| counter = message.data.counter; | |
| counterUi.innerText = counter.toString(); | |
| }); | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| swarm.onElection = (channel: Types.RealtimeChannelPromise) => { | |
| leaderUi.innerText = "true"; | |
| setInterval(() => { | |
| counter++; | |
| channel.publish("message", { counter: counter }); | |
| console.log("Sent counter", counter); | |
| }, 5000); | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| swarm.onSwarmPresenceChanged = (members: Types.PresenceMessage[]) => { | |
| presenceUi.innerText = JSON.stringify(members); | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const swarm = new Swarm("some-channel-name"); | |
| clientIdUi.innerText = swarm.id; | |
| leaderUi.innerText = "false"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Types } from "ably"; | |
| import Swarm from "./Swarm"; | |
| let counter = 0; | |
| const counterUi = document.getElementById("counter-value"); | |
| const clientIdUi = document.getElementById("client-id"); | |
| const leaderUi = document.getElementById("leader"); | |
| const presenceUi = document.getElementById("presence-members"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ... | |
| <body> | |
| <h1>Leader Election Demo</h1> | |
| <p>Client Id: <span id="client-id"></span></p> | |
| <p>Leader?: <span id="leader"></span></p> | |
| <p>Current Counter Value: <span id="counter-value"></span></p> | |
| <div id="presence-members"> | |
| Waiting to load presence.... | |
| </div> | |
| </body> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="utf-8"> | |
| <title>Example</title> | |
| <script src="./script.ts" type="module"></script> | |
| </head> | |
| ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ... | |
| 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); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private async ensureLeaderElected(members: Types.PresenceMessage[]) { | |
| const leader = members.find(s => s.data === "leader"); | |
| if (leader) { | |
| return; | |
| } | |
| ... |
NewerOlder