amplify-js: How to catch PubSub errors if MQTT connection lost or broken?

** Which Category is your question related to? ** PubSub connection problems

** What AWS Services are you utilizing? ** PubSub in aws-amplify 1.1.19

** Provide additional details e.g. code snippets ** What is the best way to keep PubSub connection alive by using aws-amplify? Or is there even any way?

I am using similar code as you mention in the documentation: PubSub.subscribe(['myTopic1','myTopic1']).subscribe({ next: data => console.log('Message received', data), error: error => console.error(error), // this never logs anything) close: () => console.log('Done'), // this never logs anything) })

However, I have the problem that clients Internet connection can be lost for a short period of time and then these can no longer receive messages. For example, if portable Wifi router switch connection from 4G to 3G, I get the following message in console: ConsoleLogger.js:88 [WARN] 14:36.405 MqttOverWSProvider - ... { "errorCode": 4, "errorMessage": "AMQJS0004E Ping timed out.", "uri": "wss://..." } I would like to catch these connection problems somehow in order to subscribe to topics again so that I can keep connection alive.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 2
  • Comments: 22 (6 by maintainers)

Most upvoted comments

In addition to AMQJS0004E Ping timed out error, AMQJS0007E Socket error:undefined (errorCode: 7) does not trigger the error. Despite the fact that it occurs when subscription fails. Documentation of PubSub says that error is “Triggered when subscription attempt fails”. However, looks like errors are not triggered…

@manueliglesias any idea when the fix for this issue is coming?

+1 request for connection event handlers and methods to retrieve current status. Same for subscriptions if not already present.

For example, in code below, no errors are thrown even if I provide non-existent region or endpoint:

Amplify.addPluggable(new AWSIoTProvider({
      aws_pubsub_region: AWSIotConfiguration.region,
      aws_pubsub_endpoint: AWSIotConfiguration.endpoint,
    }));  

However, I later receive an uncaught (in promise) error when attempting the PubSub.subscribe() action. If we could know that the connection failed in advance we could avoid the unecessary subscription attempt.

Hi @mevert

At the moment, there is no way to catch those events.

https://github.com/aws-amplify/amplify-js/blob/e7157dc797c76b66b79f069705428a0bc8e2d054/packages/pubsub/src/Providers/MqttOverWSProvider.ts#L99-L101

Which, as of today, only logs a warning:

https://github.com/aws-amplify/amplify-js/blob/e7157dc797c76b66b79f069705428a0bc8e2d054/packages/pubsub/src/Providers/MqttOverWSProvider.ts#L85-L89

If you are on a browser environment, one thing you can try is the Navigator online api to listen for connection changes and unsubscribe/re-subscribe accordingly.

I am also labeling this as a feature-request

Any update on this? I always get this error when trying to subscribe to a topic after internet is restored.

     "errorCode":7,
     "errorMessage":"AMQJS0007E Socket error:undefined."

@elorzafe thanks for the info. I will let you know if I face the problem again. What is the status with the connection handlers? And are we now able to know when IoT connection breaks in all of the possible cases?

Currently, I have used PING/PONG check for websockets in order make sure that WS connection is still alive and working because I wasn’t able to catch all broken connections with aws-amplify. So I have been calling backend Lambda that will send IoT message to the client and if client won’t receive the message in a certain amount of time it will try to initialize Websocket connections.