getPastevents crash

Hello,

I have a mongo database and I sync it with blockchain. To do that, i’m using :

MyContract.getPastEvents("allEvents", { fromBlock: mintBlock, toBlock:lastBlock }, function (error, events) { })

But the problem is, if I ask for like 4000 events, infura node crash (maybe because it’s too big) ?

(node:5976) UnhandledPromiseRejectionWarning: Error: CONNECTION ERROR: Couldn't connect to node on WS.
    at Object.ConnectionError (/path/node_modules/web3-core-helpers/lib/errors.js:66:23)
    at Object.InvalidConnection (/path/node_modules/web3-core-helpers/lib/errors.js:36:21)
    at /path/node_modules/web3-providers-ws/lib/index.js:161:37
    at Map.forEach (<anonymous>)
    at WebsocketProvider._onClose (/path/node_modules/web3-providers-ws/lib/index.js:160:28)
    at W3CWebSocket._dispatchEvent [as dispatchEvent] (/path/node_modules/yaeti/lib/EventTarget.js:115:12)
    at W3CWebSocket.onClose (/path/node_modules/websocket/lib/W3CWebSocket.js:228:10)
    at WebSocketConnection.<anonymous> (/path/node_modules/websocket/lib/W3CWebSocket.js:201:17)
    at WebSocketConnection.emit (events.js:314:20)
    at WebSocketConnection.drop (/path/node_modules/websocket/lib/WebSocketConnection.js:475:14)
    at /path/node_modules/websocket/lib/WebSocketConnection.js:303:18
    at processTicksAndRejections (internal/process/task_queues.js:79:11)
(node:5976) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)

How to get 4000 past events from a contract without crash ?

Hello @Macha. Welcome to the Infura community.

For Idle WS connections that exceed beyond an hour will get disconnected. Adding ‘pings’ to your WebSocket connection will prevent the connection from going idle. Any unrecognized requests will trigger the server to close the connection with an error message.

You can check out the filter methods. https://infura.io/docs/ethereum#tag/Filter-Methods

This error message is only when I start my server. To avoid error, I need to make diminish block interval because I can’t sync if there is few thousands of events on my smart contract.