BQ_Websocket_NodeJS
unknown
javascript
a year ago
1.3 kB
9
Indexable
const WebSocket = require('ws');
const BITQUERY_TOKEN = '*******';
const WS_URL = 'wss://streaming.bitquery.io/eap';
const client = new WebSocket(WS_URL, ['graphql-ws'], {
headers: {
Authorization: `Bearer ${BITQUERY_TOKEN}`
}
});
client.on('open', () => {
console.log('Opened connection');
const initMessage = JSON.stringify({ type: 'connection_init' });
client.send(initMessage);
setTimeout(() => {
const payload = {
id: '1',
type: 'start',
payload: {
query: `
subscription MyQuery {
Solana(network: solana) {
Blocks {
Block {
Time
Slot
}
}
}
}
`
}
};
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);
});Editor is loading...
Leave a Comment