0Pricing
Web3 & DApp Development Fundamentals · บทเรียน

การรับฟังเหตุการณ์

การสมัครรับข้อมูลนอกเชน

การรับฟังเหตุการณ์ เป็นบทเรียน Web3 & DApp Development Fundamentals ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Web3 & DApp Development Fundamentals และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Web3 & DApp Development Fundamentals มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Consuming Events Off-Chain

Events are useless until something listens for them. Off-chain apps - web front-ends, bots, and indexers - subscribe to logs to react to on-chain activity.

Libraries like ethers.js and web3.js make this straightforward.

Listening with ethers.js

With ethers.js you attach a listener using contract.on(eventName, callback). The callback receives the decoded parameters plus the raw event object.

<code>contract.on('Transfer', (from, to, amount, event) => {
    console.log(from, '->', to, amount.toString());
});</code>

Querying Past Events

To read history rather than live events, use queryFilter with a block range. This returns an array of matching past logs.

<code>const logs = await contract.queryFilter('Transfer', 0, 'latest');
logs.forEach(l => console.log(l.args.amount.toString()));</code>

Filtering by Indexed Value

Because some parameters are indexed, you can build a filter that only matches certain topics, for example transfers received by one address.

<code>const filter = contract.filters.Transfer(null, myAddress);
const received = await contract.queryFilter(filter);</code>

Listening with web3.js

The web3.js library uses an event emitter pattern via contract.events.EventName(), with data and error callbacks.

<code>contract.events.Transfer({ fromBlock: 'latest' })
  .on('data', (e) => console.log(e.returnValues))
  .on('error', console.error);</code>

WebSocket Providers

Live event subscriptions require a WebSocket connection (wss://), because HTTP providers cannot push updates. For one-off history queries an HTTP provider is fine.

<code>// const provider = new ethers.WebSocketProvider('wss://...');
// HTTP works for queryFilter, not for live .on()</code>

Block Confirmations

An event can be reorged out if its block is replaced. Wait for several confirmations before treating an event as final, especially for value transfers.

<code>contract.on('Deposit', async (acct, amount, event) => {
    await event.getTransactionReceipt();
    // optionally wait N confirmations
});</code>

Indexers and The Graph

For large dApps, a dedicated indexer such as The Graph ingests events and exposes a queryable GraphQL API. This scales far better than scanning logs in the browser.

Decoding Raw Logs

If you have raw logs, you can decode them with the contract's ABI using an interface. The signature hash in topic 0 identifies which event it is.

<code>const iface = new ethers.Interface(abi);
const parsed = iface.parseLog(rawLog);
console.log(parsed.name, parsed.args);</code>

Avoiding Duplicate Handling

Listeners may receive the same event more than once across reconnects. Track processed log identifiers (transaction hash + log index) to keep handling idempotent.

<code>const key = event.transactionHash + ':' + event.logIndex;
if (seen.has(key)) return;
seen.add(key);</code>

Cleaning Up Listeners

Remove listeners when a component unmounts to prevent memory leaks and duplicate handlers, using contract.off or removeAllListeners.

<code>function handler(from, to, amount) { /* ... */ }
contract.on('Transfer', handler);
// later:
contract.off('Transfer', handler);</code>

Quick Check

Test your understanding of listening for events.

Recap

You learned how off-chain code listens for events:

  • contract.on() for live events, queryFilter for history
  • Filters target indexed topics
  • WebSocket providers are required for live subscriptions
  • Wait for confirmations, deduplicate logs, and clean up listeners
  • Indexers like The Graph scale event queries

คำถามที่พบบ่อย

บทเรียน “การรับฟังเหตุการณ์” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การรับฟังเหตุการณ์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Web3 & DApp Development Fundamentals ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Web3 & DApp Development Fundamentals มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การรับฟังเหตุการณ์”

การสมัครรับข้อมูลนอกเชน คุณปฏิบัติ Web3 & DApp Development Fundamentals ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Web3 & DApp Development Fundamentals หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Web3 & DApp Development Fundamentals บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “การรับฟังเหตุการณ์” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Web3 & DApp Development Fundamentals นี้ได้ไหม

ได้ บทเรียน Web3 & DApp Development Fundamentals ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การประกาศเหตุการณ์
  2. พารามิเตอร์ที่จัดทำดัชนี
  3. การรับฟังเหตุการณ์
  4. เหตุการณ์เทียบกับพื้นที่จัดเก็บ
← กลับไปที่ Web3 & DApp Development Fundamentals