โ†
Previous Module
RPC Calls Visualizer

๐Ÿ‘‚ Event Subscriptions: Real-Time Blockchain

Learn how to listen for smart contract events as they happen

๐Ÿ“ก Real-Time Blockchain Events

Event subscriptions let your application listen to blockchain changes in real-time. Instead of polling (repeatedly asking "anything new?"), you open a WebSocket connection and the node pushes events to you instantly. When a token transfer happens, a swap executes, or a proposal passes, your app knows immediatelyโ€”no delay, no wasted requests. This is how Uniswap shows live trades, how Etherscan displays pending transactions, and how wallets notify you of incoming payments.

๐ŸŽฎ Interactive: WebSocket Event Stream

Simulate a live WebSocket connection to an Ethereum node. Click "Connect" to start receiving real-time events. Watch as Transfer, Approval, and Swap events stream in automatically.

Disconnected

Event Stream

๐Ÿ“ก

Connect to start receiving events

๐Ÿ”„ Polling vs Subscriptions

๐Ÿ”Polling (Old Way)

Your app asks "anything new?" every few seconds. 99% of the time: "Nope." Wastes bandwidth, increases latency, and costs money (every poll = 1 RPC call).

Example cost
Poll every 2 seconds
= 43,200 requests/day
= ~$50/month in RPC costs
๐Ÿ“กSubscriptions (Modern)

Open 1 WebSocket. Node pushes events when they happen. Instant updates, zero wasted requests, minimal cost. Used by all modern dApps.

Example cost
1 WebSocket connection
= Unlimited events
= ~$5/month in RPC costs

๐Ÿ’ป How Event Subscriptions Work

1.
Connect via WebSocket

Establish persistent connection: wss://mainnet.infura.io/ws/v3/YOUR_KEY. Unlike HTTP (request-response), WebSocket keeps channel open for bidirectional communication.

2.
Subscribe to Event

Send subscription request: eth_subscribe("logs", {address: "0x..."}). Node responds with subscription ID. You're now listening.

3.
Receive Events

When matching event occurs, node pushes it to you: {subscription: "0x123", result: {...event data}}. No polling neededโ€”instant delivery.

4.
Process & React

Your app handles event (update UI, trigger notification, execute logic). Then continues listening. One subscription = unlimited events until you disconnect.

๐Ÿ’ก Key Insight

Event subscriptions transform your dApp from reactive (checking for changes) to responsive (notified of changes). This isn't just about speedโ€”it's about architecture. Polling couples your app to arbitrary intervals (every 2 seconds? 5 seconds?). Subscriptions decouple you: events arrive when they happen, not when you check. This enables real-time UX (live price feeds, instant transaction confirmations, wallet notifications) while reducing infrastructure costs by 10x. Every production Web3 app uses subscriptions. Learn this pattern, and you'll never poll again.

โ† RPC Calls Visualizer