2023-05-12 10:35:26 +00:00
|
|
|
## Example showing how a resource restricted client may
|
|
|
|
## use lightpush to publish messages without relay
|
|
|
|
|
2024-07-09 11:14:28 +00:00
|
|
|
import chronicles, chronos, stew/byteutils, results
|
2024-07-05 22:03:38 +00:00
|
|
|
import waku/[common/logging, node/peer_manager, waku_core, 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"
|
2024-08-05 10:57:43 +00:00
|
|
|
# node-01.do-ams3.waku.test.status.im on waku.test
|
2024-07-09 15:36:12 +00:00
|
|
|
LightpushPubsubTopic = PubsubTopic("/waku/2/rs/0/0")
|
2023-05-12 10:35:26 +00:00
|
|
|
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()
|