Node API: Implement basic subscribe topic (#89)

* Fix init signature according to Node API

See https://github.com/status-im/nim-waku/blob/master/docs/api/v2/node.md

* Update docs and example

* Node API: Basic subscribe

* Modify example with subscribe handler
This commit is contained in:
Oskar Thorén 2020-07-28 16:17:50 +08:00 committed by GitHub
parent 1ca3962e8d
commit ad5d5e2401
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 13 deletions

View File

@ -15,15 +15,18 @@ import ../../waku/node/v2/wakunode2
# Loads the config in `waku/node/v2/config.nim`
let conf = WakuNodeConf.load()
# Create and start the node
#proc runBackground(conf: WakuNodeConf) {.async.} =
# init(conf)
# runForever()
# Node operations happens asynchronously
proc runBackground(conf: WakuNodeConf) {.async.} =
# Create and start the node
let node = await init(conf)
discard init(conf)
echo("Do stuff after with node")
# Subscribe to a topic
let topic = "foobar"
proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} =
info "Hit subscribe handler", topic=topic, data=data
node.subscribe(topic, handler)
discard runBackground(conf)
runForever()
# TODO Lets start with Nim Node API first

View File

@ -6,6 +6,8 @@ import
libp2p/multiaddress,
libp2p/crypto/crypto,
libp2p/protocols/protocol,
# NOTE For TopicHandler, solve with exports?
libp2p/protocols/pubsub/pubsub,
libp2p/peerinfo,
stew/shims/net as stewNet,
rpc/wakurpc,
@ -231,8 +233,13 @@ method init*(conf: WakuNodeConf): Future[WakuNode] {.async.} =
type Topic* = string
type Message* = seq[byte]
type ContentFilter* = object
contentTopic*: string
type TopicHandler* = proc(topic: Topic, message: Message)
contentTopic*: string
# TODO Update TopicHandler to take Message, not seq[byte] data
#type TopicHandler* = proc(topic: Topic, message: Message)
# Currently this is using the one in pubsub.nim, roughly:
#type TopicHandler* = proc(topic: string, data: seq[byte])
type ContentFilterHandler* = proc(contentFilter: ContentFilter, message: Message)
type HistoryQuery = object
@ -242,13 +249,16 @@ type HistoryResponse = object
xxx*: seq[byte]
method subscribe*(w: WakuNode, topic: Topic, handler: TopicHandler) =
echo "NYI"
## Subscribes to a PubSub topic. Triggers handler when receiving messages on
## this topic. TopicHandler is a method that takes a topic and a `Message`.
##
## Status: Not yet implemented.
## TODO Implement as wrapper around `waku_protocol`, and ensure Message is
## passed, not `data` field.
## Status: Partially implemented.
## TODO Ensure Message is passed, not `data` field. This means modifying
## TopicHandler.
let wakuSub = w.switch.pubSub.get()
# XXX Consider awaiting here
discard wakuSub.subscribe(topic, handler)
method subscribe*(w: WakuNode, contentFilter: ContentFilter, handler: ContentFilterHandler) =
echo "NYI"