From 4745135ee6f82f1056317c82f79912e076b371c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Thor=C3=A9n?= Date: Tue, 28 Jul 2020 16:18:30 +0800 Subject: [PATCH] Node API: Implement publish (#91) * 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 * Waku Node: Implement basic publish topic --- examples/v2/basic2.nim | 7 ++++++- waku/node/v2/wakunode2.nim | 9 +++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/examples/v2/basic2.nim b/examples/v2/basic2.nim index 0ffe527cb..05014f12e 100644 --- a/examples/v2/basic2.nim +++ b/examples/v2/basic2.nim @@ -23,10 +23,15 @@ proc runBackground(conf: WakuNodeConf) {.async.} = # Subscribe to a topic let topic = "foobar" proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} = - info "Hit subscribe handler", topic=topic, data=data + info "Hit subscribe handler", topic=topic, data=data, decoded=cast[string](data) node.subscribe(topic, handler) + # Publish to a topic + let message = cast[seq[byte]]("hello world") + node.publish(topic, message) + discard runBackground(conf) + runForever() # TODO Lets start with Nim Node API first diff --git a/waku/node/v2/wakunode2.nim b/waku/node/v2/wakunode2.nim index 14159ae0d..73a92390c 100644 --- a/waku/node/v2/wakunode2.nim +++ b/waku/node/v2/wakunode2.nim @@ -286,12 +286,13 @@ method unsubscribe*(w: WakuNode, contentFilter: ContentFilter) = ## TODO Implement. method publish*(w: WakuNode, topic: Topic, message: Message) = - echo "NYI" ## Publish a `Message` to a PubSub topic. ## - ## Status: Not yet implemented. - ## TODO Implement as wrapper around `waku_protocol`, and ensure Message is - ## passed, not `data` field. + ## Status: Partially implemented. + ## TODO: Esure Message is passed, not seq[byte] `data` field. + let wakuSub = w.switch.pubSub.get() + # XXX Consider awaiting here + discard wakuSub.publish(topic, message) method publish*(w: WakuNode, topic: Topic, contentFilter: ContentFilter, message: Message) = echo "NYI"