JS SDK 0.11, Aqua 0.3 (#53)

This commit is contained in:
folex
2021-09-13 12:32:36 +03:00
committed by GitHub
parent 04b00967f9
commit 899d049ff0
27 changed files with 4853 additions and 9968 deletions

View File

@ -0,0 +1,33 @@
-- This file demonstrates how to send events to subscribers of a topic
-- Detailed explanation can be found in the Aqua Book: https://doc.fluence.dev/aqua-book/libraries/aqua-dht#passing-data-to-subscribers
import "@fluencelabs/aqua-dht/pubsub.aqua"
import "@fluencelabs/aqua-dht/dht.aqua"
alias PeerId: string
-- Application event
data Event:
value: string
-- API that every subscriber must adhere to
-- You can think of it as an application protocol
service SubscriberAPI:
receive_event(event: Event)
func call_subscriber(sub: Record, event: Event):
-- topological move to subscriber via relay
on sub.peer_id via sub.relay_id:
-- resolve service on a subscriber
SubscriberAPI sub.service_id!
-- call function
SubscriberAPI.receive_event(event)
-- send event to every subscriber
func send_everyone(topic: string, event: Event):
on HOST_PEER_ID:
-- retrieve all subscribers of a topic
subscribers <- findSubscribers(topic)
-- iterate through them
for sub <- subscribers par:
call_subscriber(sub, event)

View File

@ -0,0 +1,3 @@
import initTopicAndSubscribe, findSubscribers from "@fluencelabs/aqua-dht/pubsub.aqua"
export initTopicAndSubscribe, findSubscribers

24
example/src/example.ts Normal file
View File

@ -0,0 +1,24 @@
import { FluencePeer } from "@fluencelabs/fluence";
import { krasnodar } from "@fluencelabs/fluence-network-environment";
import { initTopicAndSubscribe, findSubscribers } from "./generated/export";
async function main() {
// connect to the Fluence network
const peer = FluencePeer.default;
await peer.init({ connectTo: krasnodar[1] });
let topic = "myTopic";
let value = "myValue";
// create topic (if not exists) and subscribe on it
let relay = peer.connectionInfo.connectedRelay!;
await initTopicAndSubscribe(peer, topic, value, relay, null);
// find other peers subscribed to that topic
let subscribers = await findSubscribers(peer, topic);
console.log("found subscribers:", subscribers);
}
main()
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});