Unable to maintain ws connection in nodejs

NoFace
Wed, 27 Sep 2023 13:54:37 +0000
GraphQL API

Hi, this is the best I could do, it receives a couple of objects at best and then disconnects, why?

import { WebSocket, WebSocketServer } from "ws";

const BQ_API_KEY = process.env.BQ_API_KEY;

const BITQUERY_WS_URL = "wss://streaming.bitquery.io/graphql";

const bitqueryConnection = new WebSocket(BITQUERY_WS_URL, "graphql-ws");

const lps = ["0xc74b026fab49d2380ffc6f53908c2ae6d14aa3b6"];

bitqueryConnection.on("open", () => {
  console.log("Connected to Bitquery.");

  const message = JSON.stringify({
    type: "start",
    id: "1",
    payload: {
      query: `
      subscription RealTimeLP($lps: [String!]) {
        EVM(network: eth) {
            BalanceUpdates(
                where: {
                    BalanceUpdate: { Address: { in: $lps } },
                    Currency: { SmartContract: { is: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2" }}
                }
            ) {
                balance: sum(of: BalanceUpdate_Amount)
                Address: BalanceUpdate {
                    Address
                }
            }
        }
    }
            `,
      variables: { lps },
    },
    headers: {
      "X-API-KEY": BQ_API_KEY,
    },
  });

  bitqueryConnection.send(message);
});

bitqueryConnection.on("message", (data) => {
  const response = JSON.parse(data);
  if (response.type === "data") {
    // Broadcast the data to all connected clients of your local server
    console.log("Received data from Bitquery: ", response.payload.data);
  }
});

bitqueryConnection.on("close", () => {
  console.log("Disconnected from Bitquery.");
});

Please advise, ty!

5 posts - 2 participants