hasura / react-apollo-todo

A todo app with react, apollo demonstrating graphql queries, mutations and subscriptions.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Q: Is this todo app really works 'realtime'?

rrjanbiah opened this issue · comments

As per the demo https://react-apollo-todo.demo.hasura.app it seems to make use of continuous 'polling' than 'WebSocket'. If so, what is the status of this app and what are the missing items that we need to consider before adopting it to other app?

If you take a look at react-apollo-todo/react-apollo/final-app/src/apollo.js, you can see that it actually does open a WebSocket connection:

const wsLink = new WebSocketLink(
new SubscriptionClient(REALTIME_GRAPHQL_URL, {
reconnect: true,
timeout: 30000,
connectionParams: {
headers: getHeaders(token)
}
})
);

…but it then chooses to only use that connection for subscription queries, so it uses the HTTP transport mechanism for other queries:

const link = split(
// split based on operation type
({ query }) => {
const { kind, operation } = getMainDefinition(query);
return kind === "OperationDefinition" && operation === "subscription";
},
wsLink,
httpLink
);

The “polling” you’re seeing seems to be the mutation being made at regular intervals to update the current user’s “last online” time, which is using the HTTP transport mechanism. I’m not totally sure why it does that—there’s no reason it couldn’t be using the WebSocket mechanism for everything. That said, it would still have to send those mutations at regular intervals, since it’s pushing data from the client to the server, not polling the server for data. This application doesn’t do any polling to get new data from the server.

If you want to write your own application using Hasura, you can certainly use a WebSocket connection for all your queries if you want, and Hasura will push data to your client via the WebSocket for any subscription queries you make.