Hi, I have an Aws AppSync service that I would like to establish a Websocket connection with my mobile app built in React Native
I am using the libraries @apollo/client/link/ws and @subscriptions-transport-ws
this is the code that makes the connection
const { endpointGraphQL: url, region } = awsConfig.API.GraphQL;
const auth: AuthOptions = {
type: AUTH_TYPE.API_KEY,
apiKey: 'xxxxxxx',
};
const httpLink = new HttpLink({
uri: url,
});
let ack = false;
const wsClient = new SubscriptionClient(url, {
reconnect: true,
connectionCallback: () => {
ack = true;
}
});
wsClient.use([
{
applyMiddleware(options: OperationOptions, next: Function) {
if (!ack) {
throw new Error('not ready');
}
next();
}
}
]);
wsClient.onDisconnected(() => console.log('DISCONNECTED -'));
wsClient.onConnected(() => console.log('CONNECTED -'));
const retryLink = new RetryLink({
delay: {
initial: 300,
max: 60000,
jitter: true,
},
attempts: (count, operation, e) => {
if (e && e.response && e.response.status === 401) {
return false;
}
return count < 30;
}
});
const wsLink = new WebSocketLink(wsClient);
const link = ApolloLink.from([
createAuthLink({ url, region, auth }),
createSubscriptionHandshakeLink({ url, region, auth }, httpLink),
retryLink,
wsLink,
]);
const client = new ApolloClient({
cache: new InMemoryCache(),
link,
connectToDevTools: true,
});
export const getApolloClient = () => {
return client;
};
The onDisconnected event works, but I cannot fire the onConnected event. I can make the WebSocket connection work through Postman.
Blockquote is there anything that I am missing here?
Thank you!