registry/example/src/aqua/event_example.aqua

33 lines
1.1 KiB
Plaintext
Raw Normal View History

2021-07-30 14:22:00 +03:00
-- 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
2022-02-24 16:37:58 +03:00
import "@fluencelabs/registry/routing.aqua"
import "@fluencelabs/registry/registry.aqua"
import PeerId from "@fluencelabs/aqua-lib/builtin.aqua"
2021-09-02 17:43:32 +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:
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!
-- call function
2022-02-24 17:53:51 +03:00
EventAPI.receive_event(event)
2022-02-24 17:53:51 +03:00
-- send event to every peer registered on route
2022-02-24 16:37:58 +03:00
func send_everyone(route_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
records <- resolveRoute(route_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)