stacks-blockchain-api icon indicating copy to clipboard operation
stacks-blockchain-api copied to clipboard

subscribeBlocks & subscribeMempool Not Working

Open dhwndud408 opened this issue 1 year ago • 10 comments

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.

dhwndud408 avatar Oct 28 '24 14:10 dhwndud408

Did you try the code in the referenced issue?

if you specify the transports option 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/

zone117x avatar Nov 20 '24 09:11 zone117x

Did you try the code in the referenced issue?

if you specify the transports option 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.

dhwndud408 avatar Nov 21 '24 03:11 dhwndud408

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

dhwndud408 avatar Nov 21 '24 05:11 dhwndud408

This problem should be re-assigned on API Board

dhwndud408 avatar Nov 21 '24 07:11 dhwndud408

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.

zone117x avatar Nov 21 '24 14:11 zone117x

Maybe try running it for more than a day without reconnection.

dhwndud408 avatar Nov 22 '24 22:11 dhwndud408

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

zone117x avatar Nov 25 '24 15:11 zone117x

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

dhwndud408 avatar Nov 26 '24 03:11 dhwndud408

// 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

dhwndud408 avatar Nov 26 '24 11:11 dhwndud408

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

dhwndud408 avatar Dec 01 '24 10:12 dhwndud408