0

I am using onLogs (https://solana-labs.github.io/solana-web3.js/classes/Connection.html#onLogs) to listen to interactions with my program. I am connecting to the blockchain via a paid RPC and performing server-side logic depending on how someone interacted with my Solana program.

This is the only way I can think of to maintain a web2/web3 application where the client on the front-end does stuff, and my server listens to logs emitted by my program to update the db, perform business logic, etc.

However, how do I know if the onLogs connection drops randomly? How can I handle reconnecting and listening to the program again? The documentation does not provide any relevant information.

If my entire application is dependent on listening to logs emitted by my program after actions done on the blockchain, how do I make sure my business doesnt collapse because the socket connection with the onLogs event listener suddenly fails?

Is my approach just inherently flawed, is there a better way to do this? if not how do i handle when the onLogs connection does drop?

1 Answer 1

0

The web3.js Connection class will handle reconnects using its internal RpcWebsocketClient at https://github.com/solana-labs/solana-web3.js/blob/41881b220b162e6d0e6d971fddbbc68ca9fcca04/packages/library-legacy/src/connection.ts#L3220, but the logic can be flawed.

Starting with web3.js v2, though, you have a lot more flexibility for designing reconnects as you want.

You can see an example of doing that at https://github.com/solana-labs/solana-web3.js/tree/master/packages/library#message-gap-recovery -- I encourage you to read through all the docs for websocket connections!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.