Bitquery_Javascript
unknown
javascript
a year ago
1.8 kB
40
Indexable
Never
const WebSocket = require('ws'); const API_KEY = '**********************'; const WS_URL = 'wss://streaming.bitquery.io/graphql'; const client = new WebSocket(WS_URL, ['graphql-ws'], { headers: { Authorization: `Bearer ${API_KEY}` } }); client.on('open', () => { console.log('Opened connection'); // Initialization message const initMessage = JSON.stringify({ type: 'connection_init' }); client.send(initMessage); // Simulate delay as in Java code setTimeout(() => { const payload = { id: '1', type: 'start', payload: { query: ` subscription MyQueryWithVariables($network: evm_network!, $limit: Int!) { EVM(network: $network) { Transfers(limit: { count: $limit }, orderBy: { descending: Block_Number }) { Block { Number Hash } Transfer { Currency { Symbol Name } Amount } } } } `, variables: { network: 'eth', limit: 2 } } }; client.send(JSON.stringify(payload)); }, 1000); }); client.on('message', (message) => { const jsonObject = JSON.parse(message); if (jsonObject.type !== 'ka') { console.log(`Received: ${message}`); } }); client.on('close', (code, reason) => { console.log(`Closed: ${reason}`); }); client.on('error', (error) => { console.error('WebSocket Error:', error); });