Sign up for Hasura Newsletter
Loading...

Subscription

When we had initially set up Apollo, we used Apollo Boost to install the required dependencies. But subscriptions is an advanced use case which Apollo Boost does not support. So we have to install more dependencies to set up subscriptions.

+ $ npm install apollo-link-ws subscriptions-transport-ws --save

Now we need to update our ApolloClient instance to point to the subscription server.

Open src/ApolloClient.re and update the link as follows:

githubsrc/ApolloClient.re
+let connectionParams = Json.Encode.object_([("headers", headers)]);
-let link = ApolloLinks.createHttpLink(
- ~uri="https://hasura.io/learn/graphql",
- ~headers=headers,
- ()
-);
+
+let link = ApolloLinks.webSocketLink(
+ ~uri="wss://hasura.io/learn/graphql",
+ ~reconnect=true,
+ ~connectionParams=connectionParams,
+ ()
+)

Note that we are replacing HttpLink with WebSocketLink and hence all GraphQL queries go through a single websocket connection. Look at how we are passing the headers. While initializing websocket link, we need to pass headers in the headers key in the connectionParams accepted by ApolloLinks.webSocketLink.

Did you find this page helpful?
Start with GraphQL on Hasura for Free
  • ArrowBuild apps and APIs 10x faster
  • ArrowBuilt-in authorization and caching
  • Arrow8x more performant than hand-rolled APIs
Promo
footer illustration
Brand logo
© 2022 Hasura Inc. All rights reserved
Github
Titter
Discord
Facebook
Instagram
Youtube
Linkedin