Aws-mobile-appsync-sdk-js: detect when websocket connection has been closed

Created on 19 May 2018  Â·  10Comments  Â·  Source: awslabs/aws-mobile-appsync-sdk-js

Hello,

Is there a way to get the websocket subscription connection status ? I'm currently trying to detect if the aws appsync subscriptions websocket connection has ended or is still active. If the library doesn't yet support this feature, is there a way to build the aws appsync websocket url so we can manually add apollo-link-ws link ?

Thanks

feature-request

Most helpful comment

Hi @manueliglesias,

is it possible to add auto-reconnect feature as well? Or is there any chance how to reestablish connection when it fails, right now?

Thanks in advance.

All 10 comments

Hi @alez007

It is currently not possible, but I'll take it as a feature request and track it here, thanks!

As of building your own url, that is not possible since the url comes from the backend and we do some "handshake" before establishing the connection. If you want to dig into the details, this is a good starting point https://github.com/awslabs/aws-mobile-appsync-sdk-js/blob/99ebf0e451992895ed0c7490d6183fdb0da727be/packages/aws-appsync/src/client.js#L20-L35

Another important thing is that AppSync uses MQTT over WebSockets, I am not sure if apollo-link-ws can handle that.

Hi @manueliglesias,

is it possible to add auto-reconnect feature as well? Or is there any chance how to reestablish connection when it fails, right now?

Thanks in advance.

Hey guys,
we have issues with reconnecting as well. Basically, when there is an internet connection issue, the app is not able to receive any new notifications and needs a refresh to connect again.

Is there any plan or suggestion for this issue, please?
Thx! :)

I have solved this by resubscribing every time the app detects network
after being offline.

Add a listener for navigator.offline and act based on that.

Works best with a ping kind of request in addition to this.

On Fri, Jun 15, 2018, 6:53 PM Michal Å imon notifications@github.com wrote:

Hey guys,
we have issues with reconnecting as well. Basically, when there is an
internet connection issue, the app is not able to receive any new
notifications and needs a refresh to connect again.

Is there any plan or suggestion for this issue, please?
Thx! :)

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/awslabs/aws-mobile-appsync-sdk-js/issues/132#issuecomment-397696637,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAcC1kIsDL4F1BoPZYYVv6mKldlygKpJks5t8_SQgaJpZM4UFchl
.

@alez007
I had seen your pull request. Is that possible to share how to detect the app goes to offline and add listener to navigator.offline ?

@jp928 the pull request I've provided doesn't have the navigator.offline listener implemented although you can do that like this.

hello @manueliglesias do you know about the status of this issue? error is not being triggered on my subscriptions when the socket stalls and i don't have a way to know if i should reconnect or do something about it, my subscription listener is the following:

  .subscribe({ query: OnStationChangeByComputer, variables: { account_id, computer_id: id }})
  .subscribe({
    next({ data }) {
      // Action based on the subscription
    },
    error(error) {
      // i guess that i should get the connection dropped here ?
      onError(error)
    }
  })

Hi @manueliglesias, are there any updates to the issue status? Does #274 solves this? If yes, are there any docs/notes on how to use it?

We recently added a (currently) undocumented feature to subscriptions to be able to listen for "control events" It works by adding a special variable to your subscription query. And you need to make sure you filter out those control messages, since Apollo client won't understand them.

https://github.com/awslabs/aws-mobile-appsync-sdk-js/blob/082afec44adfe9156b2123cc939c8619b229c262/packages/aws-appsync/src/link/subscription-handshake-link.ts#L43

This is how it is used: (note how we resolve a promise when the "CONNECTED" event is received (Line 287)

https://github.com/awslabs/aws-mobile-appsync-sdk-js/blob/082afec44adfe9156b2123cc939c8619b229c262/packages/aws-appsync/src/deltaSync.ts#L272-L292

We recently added a (currently) undocumented feature to subscriptions to be able to listen for _"control events"_ It works by adding a special variable to your subscription query. And you need to make sure you filter out those control messages, since Apollo client won't understand them.

https://github.com/awslabs/aws-mobile-appsync-sdk-js/blob/082afec44adfe9156b2123cc939c8619b229c262/packages/aws-appsync/src/link/subscription-handshake-link.ts#L43

This is how it is used: (note how we resolve a promise when the "CONNECTED" event is received (Line 287)

https://github.com/awslabs/aws-mobile-appsync-sdk-js/blob/082afec44adfe9156b2123cc939c8619b229c262/packages/aws-appsync/src/deltaSync.ts#L272-L292

Hey bro,

Could you give a sample to show us how to use it?

Thanks
CJ

Was this page helpful?
0 / 5 - 0 ratings