Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

subscribeBlocks & subscribeMempool Not Working #2146

Open
dhwndud408 opened this issue Oct 28, 2024 · 10 comments
Open

subscribeBlocks & subscribeMempool Not Working #2146

dhwndud408 opened this issue Oct 28, 2024 · 10 comments

Comments

@dhwndud408
Copy link

dhwndud408 commented Oct 28, 2024

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.
#1885

The admin said it was resolved on above link, but it seems the issue has resurfaced with the Nakamoto update.

@github-project-automation github-project-automation bot moved this to 🆕 New in API Board Oct 28, 2024
@smcclellan smcclellan moved this from 🆕 New to 📋 Backlog in API Board Oct 28, 2024
@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in API Board Nov 2, 2024
@dhwndud408 dhwndud408 reopened this Nov 20, 2024
@zone117x
Copy link
Member

zone117x commented Nov 20, 2024

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/

@dhwndud408
Copy link
Author

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
Copy link
Author

dhwndud408 commented Nov 21, 2024

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
Copy link
Author

This problem should be re-assigned on API Board

@zone117x
Copy link
Member

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.

@dhwndud408
Copy link
Author

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

@zone117x
Copy link
Member

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

@dhwndud408
Copy link
Author

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
Copy link
Author

// 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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

No branches or pull requests

2 participants