Skip to content

Commit

Permalink
test(scaling-pubsub): initiate connections before publishing messages
Browse files Browse the repository at this point in the history
A network of peers is required for the pubsub to work correctly, and
it's not enough only to connect to a relay.
  • Loading branch information
mkermani144 committed May 6, 2024
1 parent 3cec328 commit 4647575
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions tests/scaling-pubsub/src/node/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,34 @@ const wait = () =>
setTimeout(resolve, 1 + Math.random() * 4);
});

setTimeout(() => {
setTimeout(async () => {
// send some messages to initiate connections with other peers
const messages = Array.from({ length: 10 }).map((_, index) =>
index.toString(),
);
for (const message of messages) {
for (const peer of process.env.ALL_PEER_IDS!.split(',')) {
if (peer !== process.env.NODE_PEER_ID!) {
try {
await node.sendMessage(peer, message);
console.info(`Message sent: ${message}`);
} catch (error) {
console.warn(
`tried to send a message to ${peer.slice(-5)} but failed due to error: ${error}`,
);
}
await wait();
}
}
}

// then start publishing periodically
setInterval(async () => {
for (let i = 0; i < 10; i++) {
try {
node.publish('rosenet-news', Date.now().toString());
const message = Date.now().toString();
node.publish('rosenet-news', message);
console.info(`Message published: ${message}`);
} catch (error) {
console.warn(
`tried to publish a message but failed due to error: ${error}`,
Expand Down

0 comments on commit 4647575

Please sign in to comment.