Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

detect when websocket connection has been closed #132

Closed
alez007 opened this issue May 18, 2018 · 10 comments

Comments

@alez007
Copy link

commented May 18, 2018

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

@manueliglesias

This comment has been minimized.

Copy link
Contributor

commented May 23, 2018

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

export const createSubscriptionHandshakeLink = (url, resultsFetcherLink = new HttpLink({ uri: url })) => {
return ApolloLink.split(
operation => {
const { query } = operation;
const { kind, operation: graphqlOperation } = getMainDefinition(query);
const isSubscription = kind === 'OperationDefinition' && graphqlOperation === 'subscription';
return isSubscription;
},
ApolloLink.from([
new NonTerminatingHttpLink('subsInfo', { uri: url }, true),
new SubscriptionHandshakeLink('subsInfo'),
]),
resultsFetcherLink,
);
};

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

@JiriSko

This comment has been minimized.

Copy link

commented Jun 14, 2018

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.

@michalhisim

This comment has been minimized.

Copy link

commented Jun 15, 2018

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! :)

@alez007

This comment has been minimized.

Copy link
Author

commented Jun 15, 2018

@jp928

This comment has been minimized.

Copy link

commented Aug 28, 2018

@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 ?

@alez007

This comment has been minimized.

Copy link
Author

commented Aug 28, 2018

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

@manueliglesias manueliglesias self-assigned this Sep 16, 2018

@Negan1911

This comment has been minimized.

Copy link

commented Nov 7, 2018

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:

client
  .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)
    }
  })
@LeeIakokka

This comment has been minimized.

Copy link

commented Dec 6, 2018

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?

@manueliglesias

This comment has been minimized.

Copy link
Contributor

commented Dec 12, 2018

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.

export const CONTROL_EVENTS_KEY = '@@controlEvents';

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

subscription = client.subscribe<FetchResult, any>({
query: query,
variables: {
...variables,
[SKIP_RETRY_KEY]: true,
[CONTROL_EVENTS_KEY]: true,
},
}).filter(data => {
const { extensions: { controlMsgType = undefined, controlMsgInfo = undefined } = {} } = data;
const isControlMsg = typeof controlMsgType !== 'undefined';
if (controlMsgType) {
subsControlLogger(controlMsgType, controlMsgInfo);
if (controlMsgType === 'CONNECTED') {
resolve();
}
}
return !isControlMsg;
}).subscribe({

@chengjia0807

This comment has been minimized.

Copy link

commented May 1, 2019

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.

export const CONTROL_EVENTS_KEY = '@@controlEvents';

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

subscription = client.subscribe<FetchResult, any>({
query: query,
variables: {
...variables,
[SKIP_RETRY_KEY]: true,
[CONTROL_EVENTS_KEY]: true,
},
}).filter(data => {
const { extensions: { controlMsgType = undefined, controlMsgInfo = undefined } = {} } = data;
const isControlMsg = typeof controlMsgType !== 'undefined';
if (controlMsgType) {
subsControlLogger(controlMsgType, controlMsgInfo);
if (controlMsgType === 'CONNECTED') {
resolve();
}
}
return !isControlMsg;
}).subscribe({

Hey bro,

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

Thanks
CJ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
8 participants
You can’t perform that action at this time.