2020-09-01 08:32:19 +00:00
|
|
|
## Here's a basic example of how you would start a Waku node, subscribe to
|
|
|
|
## topics, and publish to them.
|
|
|
|
|
|
|
|
import
|
|
|
|
std/os,
|
|
|
|
confutils, chronicles, chronos,
|
|
|
|
stew/shims/net as stewNet,
|
|
|
|
libp2p/crypto/[crypto,secp],
|
|
|
|
eth/keys,
|
|
|
|
json_rpc/[rpcclient, rpcserver],
|
|
|
|
../../waku/node/v2/[config, wakunode2, waku_types],
|
|
|
|
../../waku/node/common
|
2020-07-20 04:40:35 +00:00
|
|
|
|
2020-07-28 08:17:50 +00:00
|
|
|
# Node operations happens asynchronously
|
2020-09-01 08:32:19 +00:00
|
|
|
proc runBackground() {.async.} =
|
|
|
|
let
|
|
|
|
conf = WakuNodeConf.load()
|
|
|
|
(extIp, extTcpPort, extUdpPort) = setupNat(conf.nat, clientId,
|
|
|
|
Port(uint16(conf.tcpPort) + conf.portsShift),
|
|
|
|
Port(uint16(conf.udpPort) + conf.portsShift))
|
|
|
|
node = WakuNode.init(conf.nodeKey, conf.libp2pAddress,
|
|
|
|
Port(uint16(conf.tcpPort) + conf.portsShift), extIp, extTcpPort)
|
|
|
|
|
|
|
|
await node.start()
|
2020-07-28 08:17:50 +00:00
|
|
|
|
|
|
|
# Subscribe to a topic
|
|
|
|
let topic = "foobar"
|
|
|
|
proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} =
|
2020-07-28 08:18:30 +00:00
|
|
|
info "Hit subscribe handler", topic=topic, data=data, decoded=cast[string](data)
|
2020-07-28 08:17:50 +00:00
|
|
|
node.subscribe(topic, handler)
|
|
|
|
|
2020-07-28 08:18:30 +00:00
|
|
|
# Publish to a topic
|
|
|
|
let message = cast[seq[byte]]("hello world")
|
|
|
|
node.publish(topic, message)
|
|
|
|
|
2020-09-01 08:32:19 +00:00
|
|
|
# TODO Await with try/except here
|
|
|
|
discard runBackground()
|
2020-07-28 08:18:30 +00:00
|
|
|
|
2020-07-28 08:06:00 +00:00
|
|
|
runForever()
|