1+ import chronicles
2+ import chronos
3+
4+ import chat_sdk
5+ import content_types
6+
7+
8+ proc getContent (content: ContentFrame ): string =
9+ # Skip type checks and assume its a TextFrame
10+ let m = decode (content.bytes, TextFrame ).valueOr:
11+ raise newException (ValueError , fmt" Badly formed Content " )
12+ return fmt" { m} "
13+
14+ proc main () {.async .} =
15+
16+ # Create Configurations
17+ var cfg_saro = DefaultConfig ()
18+ var cfg_raya = DefaultConfig ()
19+
20+ # Cross pollinate Peers - No Waku discovery is used in this example
21+ cfg_saro.staticPeers.add (cfg_raya.getMultiAddr ())
22+ cfg_raya.staticPeers.add (cfg_saro.getMultiAddr ())
23+
24+ # Create Clients
25+ var saro = newClient (cfg_saro, createIdentity (" Saro" ))
26+ var raya = newClient (cfg_raya, createIdentity (" Raya" ))
27+
28+ # Wire Callbacks
29+ saro.onNewMessage (proc (convo: Conversation , msg: ContentFrame ) {.async .} =
30+ echo " Saro <------ :: " & getContent (msg)
31+ await sleepAsync (10000 )
32+ discard await convo.sendMessage (saro.ds, initTextFrame (" Ping" ).toContentFrame ())
33+ )
34+
35+ saro.onDeliveryAck (proc (convo: Conversation , msgId: string ) {.async .} =
36+ echo " Saro -- Read Receipt for " & msgId
37+ )
38+
39+
40+
41+
42+ raya.onNewMessage (proc (convo: Conversation , msg: ContentFrame ) {.async .} =
43+ echo " ------> Raya :: " & getContent (msg)
44+ await sleepAsync (10000 )
45+ sdiscard await convo.sendMessage (raya.ds, initTextFrame (" Pong" ).toContentFrame ())
46+ )
47+
48+ raya.onNewConversation (proc (convo: Conversation ) {.async .} =
49+ echo " ------> Raya :: New Conversation: " & convo.id ()
50+ discard await convo.sendMessage (raya.ds, initTextFrame (" Hello" ).toContentFrame ())
51+ )
52+ raya.onDeliveryAck (proc (convo: Conversation , msgId: string ) {.async .} =
53+ echo " raya -- Read Receipt for " & msgId
54+ )
55+
56+
57+ await saro.start ()
58+ await raya.start ()
59+
60+ await sleepAsync (5000 )
61+
62+ # Perform OOB Introduction: Raya -> Saro
63+ let raya_bundle = raya.createIntroBundle ()
64+ discard await saro.newPrivateConversation (raya_bundle)
65+
66+ await sleepAsync (10000 ) # Run for some time
67+
68+ saro.stop ()
69+ raya.stop ()
0 commit comments