Trigger replication on changes
By default RxDB will periodically sync from the remote GraphQL source. This time interval for the sync to be triggered can be set by using the liveInterval
parameter. In this example we have set it to 10 minutes. However, we can also use a GraphQL subscription so that Hasura notifies us as soon as there is a change and trigger replication manually. This is done by the setupGraphQLSubscription
method of the GraphQLReplicator
class.
setupGraphQLSubscription(auth, replicationState) {// Change this url to point to your hasura graphql urlconst endpointURL = 'wss://my-hasura-instance.hasura.app/v1/graphql';const wsClient = new SubscriptionClient(endpointURL, {reconnect: true,connectionParams: {headers: {'Authorization': `Bearer ${auth.idToken}`}},timeout: 1000 * 60,onConnect: () => {console.log('SubscriptionClient.onConnect()');},connectionCallback: () => {console.log('SubscriptionClient.connectionCallback:');},reconnectionAttempts: 10000,inactivityTimeout: 10 * 1000,lazy: true});const query = `subscription onTodoChanged {todos {iddeletedisCompletedtext}}`;const ret = wsClient.request({ query });ret.subscribe({next(data) {console.log('subscription emitted => trigger run');console.dir(data);replicationState.run();},error(error) {console.log('got error:');console.dir(error);}});return wsClient}
The above code uses Apollo subscription client to subscribe to changes on the todos
table from hasura. It then triggers replication whenever new data is received.
Did you find this page helpful?
Start with GraphQL on Hasura for Free
- Build apps and APIs 10x faster
- Built-in authorization and caching
- 8x more performant than hand-rolled APIs