apollographql / subscriptions-transport-ws

:arrows_clockwise: A WebSocket client + server for GraphQL subscriptions
https://www.npmjs.com/package/subscriptions-transport-ws
MIT License
1.52k stars 342 forks source link

InactivityTimeout does not has effect #810

Closed tutok closed 2 years ago

tutok commented 4 years ago

Hi,

looks like InactivityTimeout option of SubscriptionClient does not produce any effect when set to > 0.

I have code like this:

const subscription = gqlClient
      .subscribe({ query, variables })
      .subscribe(
        next,
        error,
        complete: () => { 
           subscription.unsubscribe();
------------------------^
        }
  );

when complete callback is invoked, I would like to unsubscribe and close ws client connection. I found that according to documentation setting InactivityTimeout into anything > 0 should fit perfectly here. But unfortunately, it is not. I think the bug is in the following method:

https://github.com/apollographql/subscriptions-transport-ws/blob/1578d773caeeac435b5bad3ecbbaec477b4f3380/src/client.ts#L674-L680 the this.setInactivityTimeout(); is not invoked due to lack of this.operations[opId] as it was removed earlier: https://github.com/apollographql/subscriptions-transport-ws/blob/1578d773caeeac435b5bad3ecbbaec477b4f3380/src/client.ts#L639

I think unsubscribe method should look more or less like this:

private unsubscribe(opId: string) { 
   if (this.operations[opId]) { 
     delete this.operations[opId]; 
   } 
   this.setInactivityTimeout(); 
   this.sendMessage(opId, MessageTypes.GQL_STOP, undefined); 
 } 

can you please look at it, and check it?

UchihaYuki commented 3 years ago

Any updates about this. I also find it doesn't have any effect