@@ -85,30 +85,47 @@ proc waku_relay_subscribe(
8585 callback: FFICallBack ,
8686 userData: pointer ,
8787 pubSubTopic: cstring ,
88+ contentTopic: cstring ,
8889) {.ffi .} =
89- echo " Subscribing to topic: " & $ pubSubTopic & " ..."
9090 proc onReceivedMessage (ctx: ptr FFIContext [Waku ]): WakuRelayHandler =
9191 return proc (pubsubTopic: PubsubTopic , msg: WakuMessage ) {.async .} =
9292 callEventCallback (ctx, " onReceivedMessage" ):
9393 $ JsonMessageEvent .new (pubsubTopic, msg)
9494
9595 var cb = onReceivedMessage (ctx)
9696
97- ctx.myLib[].node.subscribe (
98- (kind: SubscriptionKind .PubsubSub , topic: $ pubsubTopic),
99- handler = WakuRelayHandler (cb),
100- ).isOkOr:
97+ # If contentTopic is provided and non-empty, use ContentSub, otherwise use PubsubSub
98+ let subscription =
99+ if contentTopic != nil and len ($ contentTopic) > 0 :
100+ echo " Subscribing to content topic: " & $ contentTopic & " ..."
101+ (kind: SubscriptionKind .ContentSub , topic: $ contentTopic)
102+ else :
103+ echo " Subscribing to pubsub topic: " & $ pubSubTopic & " ..."
104+ (kind: SubscriptionKind .PubsubSub , topic: $ pubsubTopic)
105+
106+ ctx.myLib[].node.subscribe (subscription, handler = WakuRelayHandler (cb)).isOkOr:
101107 error " SUBSCRIBE failed" , error = error
102108 return err ($ error)
103109 return ok (" " )
104110
111+ # NOTE: When unsubscribing via contentTopic, this will unsubscribe from the entire
112+ # underlying pubsub topic/shard that the content topic maps to. This affects ALL
113+ # content topics on the same shard, not just the specified content topic.
105114proc waku_relay_unsubscribe (
106115 ctx: ptr FFIContext [Waku ],
107116 callback: FFICallBack ,
108117 userData: pointer ,
109118 pubSubTopic: cstring ,
119+ contentTopic: cstring ,
110120) {.ffi .} =
111- ctx.myLib[].node.unsubscribe ((kind: SubscriptionKind .PubsubSub , topic: $ pubsubTopic)).isOkOr:
121+ # If contentTopic is provided and non-empty, use ContentUnsub, otherwise use PubsubUnsub
122+ let subscription =
123+ if contentTopic != nil and len ($ contentTopic) > 0 :
124+ (kind: SubscriptionKind .ContentUnsub , topic: $ contentTopic)
125+ else :
126+ (kind: SubscriptionKind .PubsubUnsub , topic: $ pubsubTopic)
127+
128+ ctx.myLib[].node.unsubscribe (subscription).isOkOr:
112129 error " UNSUBSCRIBE failed" , error = error
113130 return err ($ error)
114131
0 commit comments