Search code examples
ethereumethers.js

WSS or HTTP provider to subscribe to event listeners on ethers.js?


Which protocol is better to use for events like on PairCreated listening? Using ethers.js library for my script, I run some tests with WebSocketProvider and JsonRpcProvider. Both type of providers are working for event listening, but I'm not sure which one I should use? Is there some advantages or disadvantages using one over the other?

While testing my script I noticed that on some occasions my script just stops running. As I understand it happens because connection/subscription for that event gets terminated for unknown reason.

Could you guys give me some hints how should I handle this error? How should I check if I still subscribed to that event and if not - reconnect.


Solution

  • I would recommend the HTTPS RPC provider. The protocol for it is far more established, and the performance gains by using WebSocket are negligible, as blocks are 15 seconds long, anyways, so data won't be real-time anyway.

    My recommendation is to just create a new HTTPS RPC provider when the existing one errors.