subscribeBlocks & subscribeMempool Not Working
Hi! dev teams.
I am making code with your api. Here is my computer info. Mac OS Sonoma14.4 Node 18.19.1 NPM 10.2.4 API version: 8.2.0
Here is the typescript code
import * as stacks from '@stacks/blockchain-api-client';
const socketUrl = "https://api.mainnet.hiro.so";
const socket = new stacks.StacksApiSocketClient({ url: socketUrl });
let client = await stacks.connectWebSocketClient('wss://api.mainnet.hiro.so/');
sub = await socket.subscribeBlocks((event) => {
console.log(event)
}
sub = await socket.subscribeBlocks((event) => {
console.log(event)
}
After about 6 hours, the websocket stops.
I also see the this report. https://github.com/hirosystems/stacks-blockchain-api/issues/1885
The admin said it was resolved on above link, but it seems the issue has resurfaced with the Nakamoto update.
Did you try the code in the referenced issue?
if you specify the
transportsoption explicitly then the code works, here's an example:let s = document.createElement('script'); s.src = "https://www.unpkg.com/@stacks/[email protected]/lib/index.umd.js"; document.head.appendChild(s); s.onload = () => { let client = new StacksBlockchainApiClient.StacksApiSocketClient({ url: 'https://api.mainnet.hiro.so/', socketOpts: { transports: ['websocket'], // <-- workaround } }); client.subscribeBlocks(block => console.log('block', block)); client.subscribeMempool(tx => console.log('mempool', tx)); };
Originally posted by @zone117x in #1885
Also, please follow these socket-io debugging instructions and include the error debug logs: https://socket.io/docs/v4/troubleshooting-connection-issues/
Did you try the code in the referenced issue?
if you specify the
transportsoption explicitly then the code works, here's an example: let s = document.createElement('script');s.src = "https://www.unpkg.com/@stacks/[email protected]/lib/index.umd.js";
document.head.appendChild(s);
s.onload = () => {
let client = new StacksBlockchainApiClient.StacksApiSocketClient({
url: 'https://api.mainnet.hiro.so/', socketOpts: { transports: ['websocket'], // <-- workaround }});
client.subscribeBlocks(block => console.log('block', block));
client.subscribeMempool(tx => console.log('mempool', tx));
};
Originally posted by @zone117x in #1885
Also, please follow these socket-io debugging instructions and include the error debug logs: https://socket.io/docs/v4/troubleshooting-connection-issues/
Thank you. I will try that.
I changed the code. (8.2.2 latest version)
const socket = new stacks.StacksApiSocketClient({
url: socketUrl,
socketOpts: {
transports: ['websocket'], // <-- workaround
}
});
// Add socket error handling
socket.socket.on("connect_error", (err) => {
console.log("Socket Connection Error:");
console.log("Error Message:", err.message);
console.log("Error Description:", err);
// Attempt to reconnect after error
setTimeout(() => {
console.log("Attempting to reconnect...");
socket.socket.connect();
}, 5000); // Wait 5 seconds before reconnecting
});
I got error
Socket Connection Error:
Error Message: timeout
Error Description: 171 | debug("connect attempt will timeout after %d", timeout);
172 | // set timer
173 | const timer = this.setTimeoutFn(() => {
174 | debug("connect attempt timed out after %d", timeout);
175 | openSubDestroy();
176 | onError(new Error("timeout"));
^
error: timeout
This problem should be re-assigned on API Board
I found a bug in the subscription reconnect logic:
client.socket.on('connect_error', error => {
console.error('Socket connection was denied by the server', error);
// > Socket connection was denied by the server Error: Invalid topic:
});
I'm opening a PR with a fix for that. However, I'm not able to reproduce the timeout related error log you're reporting.
Maybe try running it for more than a day without reconnection.
Is the client able to reconnect? How long does it take? Were you able to try these troubleshooting steps to narrow down the disconnect reason? https://socket.io/docs/v4/troubleshooting-connection-issues/#problem-the-socket-gets-disconnected
Is the client able to reconnect? How long does it take? Were you able to try these troubleshooting steps to narrow down the disconnect reason? https://socket.io/docs/v4/troubleshooting-connection-issues/#problem-the-socket-gets-disconnected
I will try that
// Add socket error handling
socket.socket.on("connect_error", (err) => {
console.error("Socket Connection Error:");
console.error("Error Message:", err.message);
console.error("Error Description:", err);
// Attempt to reconnect after error
setTimeout(() => {
console.error("Attempting to reconnect...");
socket.socket.connect();
}, 5000); // Wait 5 seconds before reconnecting
});
// Add socket error handling
socket.socket.on("disconnect", (reason) => {
console.error("disconnect Reason:", reason);
setTimeout(() => {
console.error("Attempting to reconnect...");
socket.socket.connect();
}, 5000);
});
disconnect Reason: transport close Attempting to reconnect... Socket Connection Error: Error Message: timeout Error Description: 171 | debug("connect attempt will timeout after %d", timeout); 172 | // set timer 173 | const timer = this.setTimeoutFn(() => { 174 | debug("connect attempt timed out after %d", timeout); 175 | openSubDestroy(); 176 | onError(new Error("timeout")); ^ error: timeout at /node_modules/socket.io-client/build/cjs/manager.js:176:25
When running the reconnecting, it doen't work consistantly.
Attempting to reconnect... disconnect Reason: transport close Attempting to reconnect... disconnect Reason: transport close Attempting to reconnect... disconnect Reason: transport close