nim-libp2p/docs/go-daemon/node.nim

47 lines
1.3 KiB
Nim
Raw Normal View History

2023-09-07 14:09:36 +00:00
import chronos, nimcrypto, strutils, os
import ../../libp2p/daemon/daemonapi
2024-06-13 10:30:22 +00:00
const PubSubTopic = "test-net"
2023-09-07 14:09:36 +00:00
proc main(bn: string) {.async.} =
echo "= Starting P2P node"
var bootnodes = bn.split(",")
2024-06-13 10:30:22 +00:00
var api = await newDaemonApi(
{DHTFull, PSGossipSub, WaitBootstrap}, bootstrapNodes = bootnodes, peersRequired = 1
)
2023-09-07 14:09:36 +00:00
var id = await api.identity()
echo "= P2P node ", id.peer.pretty(), " started:"
for item in id.addresses:
echo item
2024-06-13 10:30:22 +00:00
proc pubsubLogger(
api: DaemonAPI, ticket: PubsubTicket, message: PubSubMessage
): Future[bool] {.async.} =
2023-09-07 14:09:36 +00:00
let msglen = len(message.data)
2024-06-13 10:30:22 +00:00
echo "= Recieved pubsub message with length ",
msglen, " bytes from peer ", message.peer.pretty(), ": "
2023-09-07 14:09:36 +00:00
var strdata = cast[string](message.data)
echo strdata
result = true
var ticket = await api.pubsubSubscribe(PubSubTopic, pubsubLogger)
# Waiting for gossipsub interval
while true:
var peers = await api.pubsubListPeers(PubSubTopic)
if len(peers) > 0:
break
await sleepAsync(1000)
var data = "HELLO\r\n"
var msgData = cast[seq[byte]](data)
await api.pubsubPublish(PubSubTopic, msgData)
when isMainModule:
if paramCount() != 1:
echo "Please supply bootnodes!"
else:
waitFor(main(paramStr(1)))
while true:
poll()