Auth

 avatar
unknown
javascript
2 years ago
2.0 kB
4
Indexable
const WebSocket = require('ws');

const token = '********';
const WS_URL = 'wss://streaming.bitquery.io/graphql';

const client = new WebSocket(WS_URL, ['graphql-ws'], {
    header: {
        Authorization: `Bearer ${token}`
    }
});

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',  // Add the 'id' field
            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);
});
Editor is loading...
Leave a Comment