registry/example/src/example.ts

28 lines
951 B
TypeScript
Raw Normal View History

2021-09-13 13:44:30 +03:00
import { Fluence } from "@fluencelabs/fluence";
2021-07-28 13:53:38 +03:00
import { krasnodar } from "@fluencelabs/fluence-network-environment";
import { initTopicAndSubscribeBlocking, findSubscribers } from "./generated/export";
2021-07-28 13:53:38 +03:00
async function main() {
// connect to the Fluence network
2021-09-13 13:44:30 +03:00
await Fluence.start({ connectTo: krasnodar[1] });
let topic = "myTopic" + new Date().valueOf();
2021-07-28 13:53:38 +03:00
let value = "myValue";
console.log("Will create topic", topic);
2021-07-28 13:53:38 +03:00
// create topic (if not exists) and subscribe on it
2021-09-13 13:44:30 +03:00
let relay = Fluence.getStatus().relayPeerId;
await initTopicAndSubscribeBlocking(
topic, value, relay, null,
(s) => console.log(`node ${s} saved the record`)
);
2021-07-28 13:53:38 +03:00
// find other peers subscribed to that topic
2021-09-13 13:44:30 +03:00
let subscribers = await findSubscribers(topic);
2021-07-28 13:53:38 +03:00
console.log("found subscribers:", subscribers);
}
main()
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});