mirror of
https://github.com/fluencelabs/registry.git
synced 2025-06-27 23:01:36 +00:00
JS SDK 0.11, Aqua 0.3 (#53)
This commit is contained in:
33
example/src/aqua/event_example.aqua
Normal file
33
example/src/aqua/event_example.aqua
Normal 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)
|
3
example/src/aqua/export.aqua
Normal file
3
example/src/aqua/export.aqua
Normal file
@ -0,0 +1,3 @@
|
||||
import initTopicAndSubscribe, findSubscribers from "@fluencelabs/aqua-dht/pubsub.aqua"
|
||||
|
||||
export initTopicAndSubscribe, findSubscribers
|
24
example/src/example.ts
Normal file
24
example/src/example.ts
Normal 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);
|
||||
});
|
Reference in New Issue
Block a user