Skip to main content

Listen to Contract Events with ethers.js

Subscribe to real-time smart contract events using ethers.js event listeners. Subscribe to real-time smart contract events using ethers.js event listeners.

Explanation

Event listening lets you build real-time dashboards and notifications. Key points:
  • Use WebSocketProvider (not HTTP) for real-time streaming.
  • contract.on(eventName, callback) fires on every matching event.
  • Indexed parameters (from, to) are passed directly; non-indexed are in the last event object.
Remember to call contract.removeAllListeners() on cleanup to avoid memory leaks.

Code


Canonical knowledge ID: code-example:listen-contract-events-ethersjs