Topic‑filtered subscription versus broad getPastLogs with client‑side filtering for Web3.js alert noise
0 reputation · 13 Dec 2021, 11:11 UTC
0 reputation · 13 Dec 2021, 11:11 UTC
Reduce UI alert noise when monitoring a smart contract that emits frequent events, while ensuring that no relevant log is lost due to filtering or connection issues.
Using a topic‑filtered WebSocket subscription limits network traffic but depends on a stable connection and cannot filter non‑indexed parameters. A broad getPastLogs call with client‑side filtering gives full control over what triggers an alert but may increase latency and processing load, especially on busy contracts.
Uncertainty remains about which method better balances missed‑event risk and noise when the node experiences intermittent WebSocket disconnects, and how the behavior differs between web3.js v1.x and v2.x.
Which approach yields fewer noisy alerts under high event rates? How does each method recover missed events after a WebSocket reconnection? What version‑specific considerations affect implementation?
35275 reputation · 13 Dec 2021, 13:56 UTC
When monitoring a contract that emits frequent events, the choice between a topic‑filtered WebSocket subscription and a broad getPastLogs call with client‑side filtering determines both alert noise and reliability. Under high event rates, a topic‑filtered subscription typically produces fewer noisy alerts, because the node filters by indexed parameters before sending data over the wire. However, this approach cannot evaluate non‑indexed fields, and it depends on a stable WebSocket connection.
getPastLogs + client filtering: The node returns all logs for a block range, and the client applies every filter. This guarantees that no event is missed after a disconnect, but it increases RPC load, latency, and the volume of alerts the UI must evaluate.getPastLogs.getPastLogs polling or accept that those fields will arrive in every topic‑matched log.const sub = await provider.getSubscription('logs', { address: contractAddress, topics: [eventSignature] })
Web3.js v1.x subscriptions emit data and error events; reconnection and resumption must be handled manually, including re‑subscribing and issuing a targeted getPastLogs for the gap. Web3.js v2.x’s Subscription class adds automatic reconnection and resumable‑stream options when the underlying provider supports it, reducing the amount of custom reconnect logic required.
Diagnostic detail required: Are the parameters you need to filter by marked indexed in the Solidity event definition? If they are not, topic filtering is impossible, and you must rely on client‑side getPastLogs polling.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.