Sign up for Hasura Newsletter

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/main.js and update the following imports:

githubsrc/main.js
- import { HttpLink } from 'apollo-link-http';
+ import { WebSocketLink } from 'apollo-link-ws';

Update the link by removing HttpLink and adding a WebSocketLink function to integrate WebSocketLink.

import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import AuthPlugin from "./plugins/auth";
import VueApollo from 'vue-apollo'
import ApolloClient from "apollo-client";
import { WebSocketLink } from 'apollo-link-ws';
import { InMemoryCache } from "apollo-cache-inmemory";
Vue.use(AuthPlugin);
Vue.use(VueApollo);
Vue.config.productionTip = false;
const getHeaders = () => {
const headers = {};
const token = window.localStorage.getItem('apollo-token');
if (token) {
headers.Authorization = `Bearer ${token}`;
}
return headers;
};
- // Create an http link:
- const link = new HttpLink({
- uri: 'https://hasura.io/learn/graphql',
- fetch,
- headers: getHeaders()
- });
+ // Create a WebSocket link:
+ const link = new WebSocketLink({
+ uri: 'wss://hasura.io/learn/graphql',
+ options: {
+ reconnect: true,
+ timeout: 30000,
+ connectionParams: () => {
+ return { headers: getHeaders() };
+ },
+ }
+ });
const client = new ApolloClient({
link: link,
cache: new InMemoryCache({
addTypename: true
})
});
const apolloProvider = new VueApollo({
defaultClient: client,
})
new Vue({
router,
apolloProvider,
render: h => h(App)
}).$mount("#app");

Note that we are replacing HttpLink with WebSocketLink and hence all GraphQL queries go through a single websocket connection.

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
© 2021 Hasura Inc. All rights reserved
Github
Titter
Discord
Facebook
Instagram
Youtube
Linkedin