Ethers.js is a JavaScript library for interacting with the Ethereum blockchain that provides an API for working with smart contracts. You can use the contract.on function to filter events emitted by a smart contract.
When you use the contract.on function to filter events, the connection does not die when the page is refreshed. However, it's important to keep in mind that if the user navigates away from the page or closes the browser, the connection will be terminated and you will need to create a new filter.
To filter events based on a specific user address, you can use the contract.on function with a filter object that specifies the user address as the first argument of the event.
contract.on("NewEvent", { useraddress: userAddress }, (useraddress, somenumber, somestring) => {
// handle event here
});
You can also use the contract.on function with a filter object that specifies multiple arguments of the event, such as user address and somenumber.
contract.on("NewEvent", { useraddress: userAddress, somenumber: someNumber }, (useraddress, somenumber, somestring) => {
// handle event here
});
It's also worth noting that if you want to stop listening to the events, you can use the removeListener function to remove the listener from the contract.
contract.removeListener(listener);
or
contract.removeAllListeners();