docs icon indicating copy to clipboard operation
docs copied to clipboard

Better documentation around AppSync Subscription reconnection logic

Open ahansson89 opened this issue 5 years ago • 3 comments

Is your feature request related to a problem? Please describe. There is no documentation for how to resubscribe an AppSync subscription if connection failure is happening

Describe the solution you'd like Ideally, I do not want to handle this myself and just let the framework handle the reconnection.

Describe alternatives you've considered Write code that handles the exponential backoff

Additional context This is how I tried to do it, but I do not get a successful reconnect. Obviously there is no backoff implemented yet..

useEffect(() => {
    let subscription;

    const observable = API.graphql(
      graphqlOperation(onCreateChild, {
        owner: user.sub
      })
    );

    const subscriptionSettings = {
      error: e => {
        subscription.unsubscribe();
        subscription = null;
        subscription = observable.subscribe(subscriptionSettings);
        console.log("onCreateChild", JSON.stringify(e));
      },
      next: childData => {
        console.log(childData)
      }
    };

    subscription = observable.subscribe(subscriptionSettings);

    return () => {
      if (subscription) {
        console.log("unsubscribe");
        subscription.unsubscribe();
      }
    };
  }, [user.sub]);

What is the best way to handle this?

ahansson89 avatar Oct 17 '20 06:10 ahansson89

I just stumbled upon this issue as well and wondering how can I do the same. I need the onCreate Subscription to update the information for the auto-refresh functionality that is needed in my app.

This is my first GraphQL subscription with AppSync/Amplify and I'll be disappointed if this doesn't work as expected. I certainly do not expect to write code to maintain web socket connections.

Is any workaround available until this is identified as an issue or a fix is available?

stayingcool avatar Oct 27 '20 00:10 stayingcool

By the way, I'm using the GraphQL client and not the SDK.

stayingcool avatar Oct 27 '20 00:10 stayingcool

Hey @ahansson89 👋 apologies for the delay here. Since this issue speaks to the documentation specifically, I'm going to transfer it to the docs repo so that we can add examples that help to re-establish connections for GraphQL subscriptions.

chrisbonifacio avatar Jul 02 '21 16:07 chrisbonifacio

Hi we've added the documentation here: https://docs.amplify.aws/lib/graphqlapi/subscribe-data/q/platform/js/#connection-issues-and-automated-reconnection

renebrandel avatar Mar 15 '23 16:03 renebrandel