2020-07-20 04:40:35 +00:00
|
|
|
# Here's an example of how you would start a Waku node, subscribe to topics, and
|
|
|
|
# publish to them
|
|
|
|
|
|
|
|
import confutils, chronicles, chronos, os
|
|
|
|
|
2020-07-28 08:06:00 +00:00
|
|
|
import stew/shims/net as stewNet
|
2020-07-20 04:40:35 +00:00
|
|
|
import libp2p/crypto/crypto
|
|
|
|
import libp2p/crypto/secp
|
|
|
|
import eth/keys
|
|
|
|
import json_rpc/[rpcclient, rpcserver]
|
|
|
|
|
|
|
|
import ../../waku/node/v2/config
|
|
|
|
import ../../waku/node/v2/wakunode2
|
2020-08-28 09:08:28 +00:00
|
|
|
import ../../waku/node/v2/waku_types
|
2020-07-20 04:40:35 +00:00
|
|
|
|
|
|
|
# Loads the config in `waku/node/v2/config.nim`
|
|
|
|
let conf = WakuNodeConf.load()
|
|
|
|
|
2020-07-28 08:17:50 +00:00
|
|
|
# Node operations happens asynchronously
|
|
|
|
proc runBackground(conf: WakuNodeConf) {.async.} =
|
|
|
|
# Create and start the node
|
2020-07-28 10:28:32 +00:00
|
|
|
let node = await WakuNode.init(conf)
|
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-07-28 08:17:50 +00:00
|
|
|
discard runBackground(conf)
|
2020-07-28 08:18:30 +00:00
|
|
|
|
2020-07-28 08:06:00 +00:00
|
|
|
runForever()
|