openwallet-foundation/credo-ts

Mediator in Live delivery mode not getting Live Mode sessions

sairanjit opened this issue · 6 comments

When using the MediatorPickupStrategy as PickUpV2LiveMode the mobile agent is not able to receive the messages immediately

@genaris I was able to fix this by following changes in the following methods.

diff --git a/packages/core/src/modules/message-pickup/MessagePickupApi.ts b/packages/core/src/modules/message-pickup/MessagePickupApi.ts
index a5033c80..88ef418d 100644
--- a/packages/core/src/modules/message-pickup/MessagePickupApi.ts
+++ b/packages/core/src/modules/message-pickup/MessagePickupApi.ts
@@ -240,7 +240,9 @@ export class MessagePickupApi<MPPs extends MessagePickupProtocol[] = [V1MessageP
         .subscribe(replaySubject)
     }
 
-    await this.messageSender.sendMessage(outboundMessageContext)
+    await this.messageSender.sendMessage(outboundMessageContext, {
+      transportPriority: { schemes: ['wss', 'ws'], restrictive: true },
+    })
 
     if (options.awaitCompletion) {
       await firstValueFrom(replaySubject)
@@ -266,7 +268,10 @@ export class MessagePickupApi<MPPs extends MessagePickupProtocol[] = [V1MessageP
       new OutboundMessageContext(message, {
         agentContext: this.agentContext,
         connection: connectionRecord,
-      })
+      }),
+      {
+        transportPriority: { schemes: ['wss', 'ws'], restrictive: true },
+      }
     )
   }
 }

Interesting! Does your mediator expose both http and ws endpoints?

Yes

Also if I change something similar in mediatorRecipientApi also works

diff --git a/packages/core/src/modules/routing/MediationRecipientApi.ts b/packages/core/src/modules/routing/MediationRecipientApi.ts
index 43609fa6..5b28df1b 100644
--- a/packages/core/src/modules/routing/MediationRecipientApi.ts
+++ b/packages/core/src/modules/routing/MediationRecipientApi.ts
@@ -103,10 +103,11 @@ export class MediationRecipientApi {
 
   private async sendMessage(outboundMessageContext: OutboundMessageContext, pickupStrategy?: MediatorPickupStrategy) {
     const mediatorPickupStrategy = pickupStrategy ?? this.config.mediatorPickupStrategy
-    const transportPriority =
-      mediatorPickupStrategy === MediatorPickupStrategy.Implicit
-        ? { schemes: ['wss', 'ws'], restrictive: true }
-        : undefined
+    const transportPriority = [MediatorPickupStrategy.Implicit, MediatorPickupStrategy.PickUpV2LiveMode].includes(
+      mediatorPickupStrategy as MediatorPickupStrategy
+    )
+      ? { schemes: ['wss', 'ws'], restrictive: true }
+      : undefined
 
     await this.messageSender.sendMessage(outboundMessageContext, {
       transportPriority,
@@ -205,6 +206,7 @@ export class MediationRecipientApi {
           )
           try {
             if (pickupStrategy === MediatorPickupStrategy.PickUpV2LiveMode) {
+              await this.openMediationWebSocket(mediator)
               // Start Pickup v2 protocol in live mode (retrieve any queued message before)
               await this.messagePickupApi.pickupMessages({
                 connectionId: mediator.connectionId,
@@ -271,6 +273,7 @@ export class MediationRecipientApi {
         // PickUp V2 in Live Mode will retrieve queued messages and then set up live delivery mode
         this.logger.info(`Starting Live Mode pickup of messages from mediator '${mediatorRecord.id}'`)
         await this.monitorMediatorWebSocketEvents(mediatorRecord, mediatorPickupStrategy)
+        await this.openMediationWebSocket(mediatorRecord)
 
         await this.messagePickupApi.pickupMessages({
           connectionId: mediatorConnection.id,

Thanks @sairanjit ! So if your mediator supports both HTTP and WS, it seems that the client is giving priority to the first one. As a result, after each Message Pickup loop, the session is closed, so the Live mode will not work at all.

So I think we should always prioritize WebSocket in Message Pickup (this is what transportPriority.schemes does), and make it mandatory only when working in Live Mode or Implicit (this is what transportPriority.restricted does).

I'll submit a PR soon with some fixes and clarifications of this procedure. Hopefully it will cover all cases properly and cleanly.