nwaku/examples/v2/basic2.nim
Oskar Thorén a8dbf8a7b6
Bump submodules (#157)
* Upgrade all submodules

* Use stock standard_setup and remove our version

Switch no longer relies on Pubsub argument

* Fix peerId

* Add reference to WakuRelay in WakuNode

* Use WakuRelay ref directly instead of via switch

* Tweak standard switch sig

* Fix start_network peerid

* Import nim-libp2p utils test

* Use WakuRelay in test utils

* Fix utils imports

* Tweak

* Fix trigger self test

* Disable broken filter test

* Fix and amend logscope topics

* Make subscribe calls async to use await

* Add debug stuff to nimble file

* Await for subscribe content

* Sleeping in tests

* Local checkout fixes

* XXX: Try to use .PubSub on WakuRelay

* Revert "XXX: Try to use .PubSub on WakuRelay"

This reverts commit 3a3139e4cfbb5ae9500fd30b2e79c676ccc4a53b.

* Only using gossip seems to work

Subscribe for floodsub broken

* Fix await in examples

* Get rid of double publish, still need sleep
2020-09-16 12:23:10 +08:00

46 lines
1.4 KiB
Nim

## 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
type
Topic* = waku_types.Topic
# Node operations happens asynchronously
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()
# Subscribe to a topic
let topic = cast[Topic]("foobar")
proc handler(topic: Topic, data: seq[byte]) {.async, gcsafe.} =
let message = WakuMessage.init(data).value
let payload = cast[string](message.payload)
info "Hit subscribe handler", topic=topic, payload=payload, contentTopic=message.contentTopic
await node.subscribe(topic, handler)
# Publish to a topic
let payload = cast[seq[byte]]("hello world")
let message = WakuMessage(payload: payload, contentTopic: "foo")
node.publish(topic, message)
# TODO Await with try/except here
discard runBackground()
runForever()