3

I'm doing a graphQL subscription and I need to catch the moment when a client disconnects (intentionally or by timeout) and do some work like clean caches and unsubscribe RX subscriptions. Is that possible?

I'm using graphql-spring-boot-starter with graphql-java-tools.

Here is my code:

@Component
public class Subscription implements GraphQLSubscriptionResolver {
  
  @Autowired
  InnerService innerService;

    public Publisher<Score> talkScores(final String title) {
        Observable<Score> observable = Observable.create(e -> {
          innerService.internalSubscription(e);
        });
        ConnectableObservable connectableObservable = observable.share().publish();
        connectableObservable.connect();
        return connectableObservable.toFlowable(BackpressureStrategy.BUFFER);
    }

    public void cleanSubscriptions() {
      innerService.clean()
    }
}

How to run cleanSubscriptions() when a client disconnects?

Tony G
  • 83
  • 7
  • I'm facing the same scenario... did you manage to get a solution for this? – djeison Jun 04 '21 at 21:40
  • @djeison Nope, I had to write my own implementation using graphql-java and spring-boot-starter-websocket libraries. – Tony G Jun 15 '21 at 09:30
  • 1
    @toxa-oconnor thank you for your reply. I actually was able to achieve this using ApolloSubscriptionConnectionListener which has 'onStart' and 'onStop' hook methods which get called on subscription connections. – djeison Jun 15 '21 at 16:22
  • 1
    here is the javadoc: https://javadoc.io/doc/com.graphql-java-kickstart/graphql-java-kickstart/latest/graphql/kickstart/execution/subscriptions/apollo/ApolloSubscriptionConnectionListener.html – djeison Jun 15 '21 at 16:23
  • @djeison I tried this method, but the `onClose()` and `onTerminate()` only get invoked if the client explicitly sends those apollo messages. If a client just disconnects without warning, none of the Listener methods are triggered – kane Jul 22 '22 at 02:50

0 Answers0