๐ Event Subscriptions: Real-Time Blockchain
Learn how to listen for smart contract events as they happen
Your Progress
0 / 5 completed๐ก 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.
Event Stream
Connect to start receiving events
๐ Polling vs Subscriptions
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).
= 43,200 requests/day
= ~$50/month in RPC costs
Open 1 WebSocket. Node pushes events when they happen. Instant updates, zero wasted requests, minimal cost. Used by all modern dApps.
= Unlimited events
= ~$5/month in RPC costs
๐ป How Event Subscriptions Work
Establish persistent connection: wss://mainnet.infura.io/ws/v3/YOUR_KEY. Unlike HTTP (request-response), WebSocket keeps channel open for bidirectional communication.
Send subscription request: eth_subscribe("logs", {address: "0x..."}). Node responds with subscription ID. You're now listening.
When matching event occurs, node pushes it to you: {subscription: "0x123", result: {...event data}}. No polling neededโinstant delivery.
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.