0

Am trying to use subscription on my client app to send JWT Token and refreshToken to the server using WebSocketLink connectionParams. But the problem is that my token and refreshToken are saved inside AsynStorage that require me to use async await to get the value out. When i tried to use await, it send undefined to the server.

Here is the code below:

// Create a WebSocket link:
const wsLink = new WebSocketLink({
    uri: wsUri,
    options: {
        reconnect: true,
        connectionParams: async () => {
            return {
                token: await AsyncStorage.getItem('token'),
                refreshToken: await AsyncStorage.getItem('refreshToken')
            }
        }
    }
});

If i set a plan key, it will work. But i need to get the tokens form Async storage using await. How can i go about it?

Emmanuel
  • 121
  • 5
  • In the latest version `0.9.8` of `subscriptions-transport-ws`, it still doesn't support `connectionParams` return a promise. But it had a pull request relate to this issue, and you can refer it https://github.com/apollographql/subscriptions-transport-ws/pull/348 – tuan.tran Apr 26 '18 at 04:21
  • @TuấnTrầnDuy. Thanks for the link. but it has not being merged yet. Can i edit the code in my node_module and check it out if it can make it work? – Emmanuel Apr 26 '18 at 09:57
  • You can fork and edit it yourself or just change some code of `SubscriptionClient.prototype.connect` in `client` file in `node_modules` – tuan.tran Apr 26 '18 at 10:01
  • I just tried changing the file now but what's in the pull request is normal Js code but what i found is typescript. And the PR make reference to src/client.ts but what i found in the folder is dist/client.js – Emmanuel Apr 26 '18 at 10:23
  • Yeah, you can change in `dist/client.js`, just found `SubscriptionClient.prototype.connect` and edit it like the way that pull request does. But you should fork that project, edit and build for long term using. – tuan.tran Apr 26 '18 at 15:51
  • I've got it working. Thanks for the help! – Emmanuel Apr 26 '18 at 21:24

0 Answers0