2023-05-12 10:35:26 +00:00
|
|
|
## Example showing how a resource restricted client may
|
|
|
|
## use lightpush to publish messages without relay
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
import chronicles, chronos, stew/byteutils, stew/results
|
2023-05-12 10:35:26 +00:00
|
|
|
import
|
|
|
|
../../../waku/common/logging,
|
2023-08-09 17:11:50 +00:00
|
|
|
../../../waku/node/peer_manager,
|
|
|
|
../../../waku/waku_core,
|
|
|
|
../../../waku/waku_lightpush/client
|
2023-05-12 10:35:26 +00:00
|
|
|
|
|
|
|
const
|
2024-03-15 23:08:47 +00:00
|
|
|
LightpushPeer =
|
2024-03-11 13:48:20 +00:00
|
|
|
"/ip4/178.128.141.171/tcp/30303/p2p/16Uiu2HAkykgaECHswi3YKJ5dMLbq2kPVCo89fcyTd38UcQD6ej5W"
|
|
|
|
# node-01.do-ams3.waku.test.statusim.net on waku.test
|
2023-05-12 10:35:26 +00:00
|
|
|
LightpushPubsubTopic = PubsubTopic("/waku/2/default-waku/proto")
|
|
|
|
LightpushContentTopic = ContentTopic("/examples/1/light-pubsub-example/proto")
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
proc publishMessages(
|
|
|
|
wlc: WakuLightpushClient,
|
|
|
|
lightpushPeer: RemotePeerInfo,
|
|
|
|
lightpushPubsubTopic: PubsubTopic,
|
|
|
|
lightpushContentTopic: ContentTopic,
|
|
|
|
) {.async.} =
|
2023-05-12 10:35:26 +00:00
|
|
|
while true:
|
|
|
|
let text = "hi there i'm a lightpush publisher"
|
2024-03-15 23:08:47 +00:00
|
|
|
let message = WakuMessage(
|
|
|
|
payload: toBytes(text), # content of the message
|
|
|
|
contentTopic: lightpushContentTopic, # content topic to publish to
|
|
|
|
ephemeral: true, # tell store nodes to not store it
|
|
|
|
timestamp: getNowInNanosecondTime(),
|
|
|
|
) # current timestamp
|
2023-05-12 10:35:26 +00:00
|
|
|
|
|
|
|
let wlpRes = await wlc.publish(lightpushPubsubTopic, message, lightpushPeer)
|
|
|
|
|
|
|
|
if wlpRes.isOk():
|
2024-03-15 23:08:47 +00:00
|
|
|
notice "published message using lightpush", message = message
|
2023-05-12 10:35:26 +00:00
|
|
|
else:
|
2024-03-15 23:08:47 +00:00
|
|
|
notice "failed to publish message using lightpush", err = wlpRes.error()
|
2023-05-12 10:35:26 +00:00
|
|
|
|
|
|
|
await sleepAsync(5000) # Publish every 5 seconds
|
|
|
|
|
|
|
|
proc setupAndPublish(rng: ref HmacDrbgContext) =
|
|
|
|
let lightpushPeer = parsePeerInfo(LightpushPeer).get()
|
|
|
|
|
2024-05-17 14:28:54 +00:00
|
|
|
setupLog(logging.LogLevel.NOTICE, logging.LogFormat.TEXT)
|
2023-05-12 10:35:26 +00:00
|
|
|
notice "starting lightpush publisher"
|
|
|
|
|
|
|
|
var
|
|
|
|
switch = newStandardSwitch()
|
|
|
|
pm = PeerManager.new(switch)
|
|
|
|
wlc = WakuLightpushClient.new(pm, rng)
|
|
|
|
|
|
|
|
# Start maintaining subscription
|
2024-03-15 23:08:47 +00:00
|
|
|
asyncSpawn publishMessages(
|
|
|
|
wlc, lightpushPeer, LightpushPubsubTopic, LightpushContentTopic
|
|
|
|
)
|
2023-05-12 10:35:26 +00:00
|
|
|
|
|
|
|
when isMainModule:
|
|
|
|
let rng = newRng()
|
|
|
|
setupAndPublish(rng)
|
|
|
|
runForever()
|