aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.44k stars 2.13k forks source link

Force creation of a new websocket for subscriptions when switching between APIs #7156

Open raphaelfavier opened 4 years ago

raphaelfavier commented 4 years ago

Is your feature request related to a problem? Please describe. My react client switches between 2 AppSync API endpoints using Amplify.configure

When connecting to the first API everything works: mutations are effective and subscriptions receive updates. But if I switch to the second API only mutations will work. Subscriptions will not receive any updates.

I tracked this down to amplify keeping the websocket connection alive for 1sec after unsubscribing, therefore re-using the first API's websocket connection for the second API's subscriptions.

PS: don't know if it's relevant but I'm using API_KEY for authentication.

Describe the solution you'd like I would like to be able to force amplify to close existing websocket connections when calling Amplify.configure. Otherwise make sure that new subscriptions that use "old" websocket connections do get triggered.

Describe alternatives you've considered For now I have to force my components to wait for a few seconds before creating subscriptions. This gives amplify time to detect existing websockets timeout and close them, forcing a new websocket to be created. This is quite hacky as I need to artificially go around the async nature of React.

Additional context Some observations:


- Amplify real time provider waits for 1000ms for the connection to timeout before closing the websocket:
https://github.com/aws-amplify/amplify-js/blob/61f7478609fce7dd2f25c562aeb887d3f3db4a67/packages/pubsub/src/Providers/AWSAppSyncRealTimeProvider.ts#L386
private _removeSubscriptionObserver(subscriptionId) {
    this.subscriptionObserverMap.delete(subscriptionId);

    // Verifying 1000ms after removing subscription in case there are new subscription unmount/mount
    setTimeout(this._closeSocketIfRequired.bind(this), 1000);
}

- If given enough time between API endpoint switch, Amplify will create a new websocket connection.
In such case subscriptions will work for both endpoints:
![image](https://user-images.githubusercontent.com/1252156/98677844-ebcddd00-235d-11eb-8ff3-eec3d574ba1e.png)

- Otherwise it will reuse the same connection for a different API endpoint
![image](https://user-images.githubusercontent.com/1252156/98678423-d4432400-235e-11eb-81a0-8abbb3e2b03f.png)
raphaelfavier commented 4 years ago

Hello,

is there any update/progress on the issue?

thanks