2021-07-30 14:22:00 +03:00
|
|
|
-- This file demonstrates how to send events to subscribers of a topic
|
2021-07-30 14:21:37 +03:00
|
|
|
-- Detailed explanation can be found in the Aqua Book: https://doc.fluence.dev/aqua-book/libraries/aqua-dht#passing-data-to-subscribers
|
|
|
|
|
2022-04-11 16:24:51 +04:00
|
|
|
import "@fluencelabs/registry/registry-service.aqua"
|
|
|
|
import "@fluencelabs/registry/resources-api.aqua"
|
2022-02-24 16:37:58 +03:00
|
|
|
import PeerId from "@fluencelabs/aqua-lib/builtin.aqua"
|
2021-09-02 17:43:32 +03:00
|
|
|
|
2021-07-30 14:21:37 +03:00
|
|
|
-- Application event
|
|
|
|
data Event:
|
|
|
|
value: string
|
|
|
|
|
|
|
|
-- API that every subscriber must adhere to
|
|
|
|
-- You can think of it as an application protocol
|
2022-02-24 17:53:51 +03:00
|
|
|
service EventAPI:
|
2021-07-30 14:21:37 +03:00
|
|
|
receive_event(event: Event)
|
|
|
|
|
2022-02-24 17:53:51 +03:00
|
|
|
func notify_peer(rec: Record, event: Event):
|
|
|
|
-- topological move to peer via relay
|
|
|
|
on rec.peer_id via rec.relay_id:
|
|
|
|
-- resolve service on a peer
|
|
|
|
EventAPI rec.service_id!
|
2021-07-30 14:21:37 +03:00
|
|
|
-- call function
|
2022-02-24 17:53:51 +03:00
|
|
|
EventAPI.receive_event(event)
|
2021-07-30 14:21:37 +03:00
|
|
|
|
2022-02-24 17:53:51 +03:00
|
|
|
-- send event to every peer registered on route
|
2022-04-11 16:24:51 +04:00
|
|
|
func send_everyone(key_id: string, event: Event, ack: i16):
|
2021-09-13 12:32:36 +03:00
|
|
|
on HOST_PEER_ID:
|
2022-02-24 17:53:51 +03:00
|
|
|
-- retrieve all peers registered to the route
|
2022-04-11 16:24:51 +04:00
|
|
|
records <- resolveProviders(key_id, ack)
|
2021-09-13 12:32:36 +03:00
|
|
|
-- iterate through them
|
2022-02-24 17:53:51 +03:00
|
|
|
for rec <- records par:
|
|
|
|
notify_peer(rec, event)
|