mirror of
https://github.com/logos-messaging/logos-messaging-nim.git
synced 2026-01-04 06:53:12 +00:00
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:
parent
f938fc9efb
commit
4460adf7bf
@ -15,15 +15,18 @@ import ../../waku/node/v2/wakunode2
|
|||||||
# Loads the config in `waku/node/v2/config.nim`
|
# Loads the config in `waku/node/v2/config.nim`
|
||||||
let conf = WakuNodeConf.load()
|
let conf = WakuNodeConf.load()
|
||||||
|
|
||||||
# Create and start the node
|
# Node operations happens asynchronously
|
||||||
#proc runBackground(conf: WakuNodeConf) {.async.} =
|
proc runBackground(conf: WakuNodeConf) {.async.} =
|
||||||
# init(conf)
|
# Create and start the node
|
||||||
# runForever()
|
let node = await init(conf)
|
||||||
|
|
||||||
discard init(conf)
|
# Subscribe to a topic
|
||||||
|
let topic = "foobar"
|
||||||
echo("Do stuff after with node")
|
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()
|
runForever()
|
||||||
|
|
||||||
# TODO Lets start with Nim Node API first
|
# TODO Lets start with Nim Node API first
|
||||||
|
|||||||
@ -6,6 +6,8 @@ import
|
|||||||
libp2p/multiaddress,
|
libp2p/multiaddress,
|
||||||
libp2p/crypto/crypto,
|
libp2p/crypto/crypto,
|
||||||
libp2p/protocols/protocol,
|
libp2p/protocols/protocol,
|
||||||
|
# NOTE For TopicHandler, solve with exports?
|
||||||
|
libp2p/protocols/pubsub/pubsub,
|
||||||
libp2p/peerinfo,
|
libp2p/peerinfo,
|
||||||
stew/shims/net as stewNet,
|
stew/shims/net as stewNet,
|
||||||
rpc/wakurpc,
|
rpc/wakurpc,
|
||||||
@ -231,8 +233,13 @@ method init*(conf: WakuNodeConf): Future[WakuNode] {.async.} =
|
|||||||
type Topic* = string
|
type Topic* = string
|
||||||
type Message* = seq[byte]
|
type Message* = seq[byte]
|
||||||
type ContentFilter* = object
|
type ContentFilter* = object
|
||||||
contentTopic*: string
|
contentTopic*: string
|
||||||
type TopicHandler* = proc(topic: Topic, message: Message)
|
|
||||||
|
# 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 ContentFilterHandler* = proc(contentFilter: ContentFilter, message: Message)
|
||||||
|
|
||||||
type HistoryQuery = object
|
type HistoryQuery = object
|
||||||
@ -242,13 +249,16 @@ type HistoryResponse = object
|
|||||||
xxx*: seq[byte]
|
xxx*: seq[byte]
|
||||||
|
|
||||||
method subscribe*(w: WakuNode, topic: Topic, handler: TopicHandler) =
|
method subscribe*(w: WakuNode, topic: Topic, handler: TopicHandler) =
|
||||||
echo "NYI"
|
|
||||||
## Subscribes to a PubSub topic. Triggers handler when receiving messages on
|
## Subscribes to a PubSub topic. Triggers handler when receiving messages on
|
||||||
## this topic. TopicHandler is a method that takes a topic and a `Message`.
|
## this topic. TopicHandler is a method that takes a topic and a `Message`.
|
||||||
##
|
##
|
||||||
## Status: Not yet implemented.
|
## Status: Partially implemented.
|
||||||
## TODO Implement as wrapper around `waku_protocol`, and ensure Message is
|
## TODO Ensure Message is passed, not `data` field. This means modifying
|
||||||
## passed, not `data` field.
|
## TopicHandler.
|
||||||
|
|
||||||
|
let wakuSub = w.switch.pubSub.get()
|
||||||
|
# XXX Consider awaiting here
|
||||||
|
discard wakuSub.subscribe(topic, handler)
|
||||||
|
|
||||||
method subscribe*(w: WakuNode, contentFilter: ContentFilter, handler: ContentFilterHandler) =
|
method subscribe*(w: WakuNode, contentFilter: ContentFilter, handler: ContentFilterHandler) =
|
||||||
echo "NYI"
|
echo "NYI"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user