Subscriptions
Realtime sync to your database collections
To subscribe to any collection in a Hasura server with dynamic parameters using GraphQL subscriptions, you can use JavaScript and the graphql-ws
library. This setup allows you to receive real-time updates for a generic collection specified by the user, with dynamic filtering criteria.
First, ensure you have the necessary dependencies installed. If they aren't already set up, you can install them with npm or yarn:
Next, create a JavaScript file that establishes a WebSocket connection and sets up subscriptions to a specified collection with dynamic parameters:
Replace 'wss://your-hasura-instance.uncodie.com/v1/graphql'
with the URL of your Hasura instance and adjust 'your-access-token'
with your API access token. Modify your_collection_name
and your_filter_type
according to the specific collection and filtering criteria you need to use. The dynamicField
and dynamicValue
in the variables
object should be replaced with the actual field and value you want to filter by.
This script dynamically subscribes to updates from any specified collection and filters based on the provided criteria. Whenever there is a change in the database that meets these filters, you will receive notifications through the WebSocket, and the next
function will be triggered to handle the incoming data.
Adjust the query and variables as necessary to align with your Hasura schema's specific details and the data fields you need to monitor. This approach allows for great flexibility in handling various collections and dynamic querying conditions.
Last updated
Was this helpful?