NoFace
September 27, 2023, 1:54pm
1
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!
NoFace
September 27, 2023, 2:13pm
2
Ok, solved it, please advise if this is correct
import { WebSocket } from "ws";
const BQ_API_KEY = process.env.BQ_API_KEY;
const BITQUERY_WS_URL = "wss://streaming.bitquery.io/graphql";
const lps = ["0xc74b026fab49d2380ffc6f53908c2ae6d14aa3b6"];
let attempt = 0;
function startBitqueryConnection() {
const bitqueryConnection = new WebSocket(BITQUERY_WS_URL, "graphql-ws");
bitqueryConnection.on("open", () => {
console.log("Connected to Bitquery.");
attempt = 0;
// Sending the initialisation message immediately
const initMessage = JSON.stringify({
type: "connection_init",
payload: {},
});
bitqueryConnection.send(initMessage);
setTimeout(() => {
// Then, send the subscription message after a short delay to ensure initialization is processed
const message = getSubscriptionMessage(lps);
bitqueryConnection.send(message);
}, 1000);
});
bitqueryConnection.on("message", (data) => {
const response = JSON.parse(data);
if (response.type === "data") {
console.log(
"Received data from Bitquery:",
JSON.stringify(response.payload.data, null, 2)
);
}
});
bitqueryConnection.on("close", (code, reason) => {
console.log(`Disconnected from Bitquery. Code: ${code}, Reason: ${reason}`);
const delay = Math.min(30000, Math.pow(2, attempt++) * 1000);
console.log(`Reconnecting in ${delay / 1000} seconds...`);
setTimeout(startBitqueryConnection, delay);
});
bitqueryConnection.on("error", (error) => {
console.log("Error with Bitquery WebSocket connection:", error);
});
}
function getSubscriptionMessage(lps) {
return JSON.stringify({
type: "start",
id: "1",
payload: {
query: `
subscription {
EVM(network: eth) {
Blocks {
Block {
Number
}
}
}
}
`,
variables: {},
},
headers: {
"X-API-KEY": BQ_API_KEY,
},
});
}
startBitqueryConnection();
Omkar
September 27, 2023, 2:16pm
3
Hello…Is this example working for you ?
Bitquery_Javascript (6dst9h4j) - PasteCode.io
I have tested it, it never disconnects…
Omkar
September 27, 2023, 2:32pm
4
I modified you code.
const { WebSocket, WebSocketServer } = require("ws");
const BQ_API_KEY = 'dfd';
const BITQUERY_WS_URL = "wss://streaming.bitquery.io/graphql";
const bitqueryConnection = new WebSocket(BITQUERY_WS_URL, ["graphql-ws"], {
headers: {
"Authorization": `Bearer ${BQ_API_KEY}`
}
});
const lps = ["0xc74b026fab49d2380ffc6f53908c2ae6d14aa3b6"];
bitqueryConnection.on("open", () => {
console.log("Connected to Bitquery.");
// Send initialization message
const initMessage = JSON.stringify({ type: 'connection_init' });
bitqueryConnection.send(initMessage);
// After initialization, send the actual subscription message
setTimeout(() => {
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 },
}
});
bitqueryConnection.send(message);
}, 1000);
});
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.");
});
bitqueryConnection.on('error', (error) => {
console.error('WebSocket Error:', error);
});
It works for me ( It do not get disconnected )
Can you please try this ?
NoFace
September 27, 2023, 3:37pm
5
Ok, this works now! what was the change?
I need help with the query, I need it to return the entire balance not just the update for the block, right now it returns the balance updates on the block, but I actually need the whole lp balance with the latest block, what can I do?
ty!