Skip to main content

Example: Real-Time Subscriptions

Implement real-time position monitoring with subscriptions.

Subscription​

Query

subscription WatchPositions(
  $cursor: [positions_stream_cursor_input]!
  $accountId: String
  $batchSize: Int!
) {
  positions_stream(
    cursor: $cursor
    batch_size: $batchSize
    where: {
      account_id: { _eq: $accountId }
      shares: { _gt: "0" }
    }
  ) {
    id
    shares
    vault {
      term_id
      current_share_price
    }
  }
}

Variables

Click "Run Query" to execute the GraphQL query and see results

Implementation​

import { createClient } from 'graphql-ws'

const client = createClient({
url: 'wss://mainnet.intuition.sh/v1/graphql'
})

client.subscribe(
{
query: subscription,
variables: {
cursor: [{ initial_value: { created_at: '2024-12-01T00:00:00Z' }, ordering: 'ASC' }],
accountId: '0x...',
batchSize: 10
}
},
{
next: (data) => {
console.log('Update:', data)
// Update UI with new position data
},
error: (error) => {
console.error('Subscription error:', error)
},
complete: () => {
console.log('Subscription complete')
}
}
)