Web3 how to keep connection to web socket

Im trying to listen to Transfer events but it works for couple of minutes then process terminates. I believe thats because of the blockchain node I use but not sure. Can't find anything other so.

How can I keep the connection and Listen to Transfer events 24/7

  const web3 = new Web3(new Web3.providers.WebsocketProvider('wss://bsc-ws-node.nariox.org:443'))
  const contract = await new web3.eth.Contract(
    ABI,
    contracts[0]
  )

  contract.events
    .Transfer({
      fromBlock: 'latest',
      filter: { from: contracts[1] }
    })
    .on('data', async (event: EventData) => {
      const {
        transactionHash,
        returnValues: { value }
      } = event
       ....
    })
    


Read more here: https://stackoverflow.com/questions/68477822/web3-how-to-keep-connection-to-web-socket

Content Attribution

This content was originally published by Mugetsu at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: