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

Expose ws.onclose method to client #67

Closed
sOm2y opened this issue Dec 12, 2024 · 2 comments
Closed

Expose ws.onclose method to client #67

sOm2y opened this issue Dec 12, 2024 · 2 comments

Comments

@sOm2y
Copy link

sOm2y commented Dec 12, 2024

when there aren't many events on subscribed address the websocket seems to close the session probably due to default os/firewall/network setting tcp keep alive: 7200 - 2 hours
For example, I use this function to monitor/subscribe my block rewards when my validator has been elected in current epoch, I can see events coming in but when it's not elected and you have to wait 12 hours to be elected again, the websocket timed out already, have to manually restart the process.

const { next, close } = await client.blockchainStreams.subscribeForLogsByAddressesAndTypes(
      {
        addresses: [validatorAddresss],
        types: [], 
        withMetadata: true,
      },
      { once: false }
    )

proposed way to mitigate that:
expose ws.onclose to client, so we can pass callback function to reset the subscribe function

   ws.onclose = function(){
        setTimeout(subscribeFunctionAgain, 1000);
    };
@sOm2y sOm2y changed the title websocket timed-out on subscribe methods Expose ws.onclose method to client Dec 12, 2024
@onmax
Copy link
Owner

onmax commented Dec 20, 2024

Hey, just released v0.5.5. ea39457

Now you should be able to access the WebSocket instance as follows

const { next, close, ws } = await client.blockchainStreams.subscribeForLogsByAddressesAndTypes(...)

ws.onclose()

Let me know if this solution works for you, specially if you find any issues with the types. Thank you!

PS: I am planning to refactor the WebClient implementation to use a WebSocket library to take advantage of features like auto-reconnect, timeout... You can follow #64 to find out when this will be available :)

@sOm2y
Copy link
Author

sOm2y commented Dec 21, 2024

Hey, just released v0.5.5. ea39457

Now you should be able to access the WebSocket instance as follows

const { next, close, ws } = await client.blockchainStreams.subscribeForLogsByAddressesAndTypes(...)

ws.onclose()

Let me know if this solution works for you, specially if you find any issues with the types. Thank you!

PS: I am planning to refactor the WebClient implementation to use a WebSocket library to take advantage of features like auto-reconnect, timeout... You can follow #64 to find out when this will be available :)

thanks @onmax great work!

@sOm2y sOm2y closed this as completed Dec 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants