> For the complete documentation index, see [llms.txt](https://docs.uncodie.com/1.0/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.uncodie.com/1.0/agentbase/database/subscriptions.md).

# 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:

```bash
npm install graphql graphql-ws
```

Next, create a JavaScript file that establishes a WebSocket connection and sets up subscriptions to a specified collection with dynamic parameters:

```javascript
const { createClient } = require('graphql-ws');

const client = createClient({
  url: 'wss://your-hasura-instance.uncodie.com/v1/graphql',
  connectionParams: {
    headers: {
      'Authorization': 'Bearer your-access-token'
    }
  }
});

// Replace `collection_name` with your actual collection name
const collectionName = 'your_collection_name';

const query = `
  subscription getCollectionData($filter: your_filter_type!) {
    ${collectionName}(where: $filter) {
      field1
      field2
      // Add more fields as needed
    }
  }
`;

// Define your dynamic filter here, adjust `your_filter_type` to match your schema
const variables = {
  filter: {
    dynamicField: { _eq: "dynamicValue" }
  }
};

client.subscribe(
  { query, variables },
  {
    next(data) {
      console.log('Data received:', data);
    },
    error(err) {
      console.error('Error:', err);
    },
    complete() {
      console.log('Subscription complete');
    },
  }
);
```

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.
