Event-driven architecture in Web3
How user actions trigger contract events that act as asynchronous pipelines to update off-chain indexers and notification caches.

In a standard centralized Web2 system, when User A likes User B's post, the server updates the database and immediately pushes a socket notification to User B:
This is synchronous and tightly coupled.
In Web3, the blockchain is asynchronous and disconnected from standard web servers. The blockchain cannot make HTTP requests, send push notifications, or wake up a device.
To build interactive features, you must design an event-driven loop:
1. The On-Chain Event Trigger
Solidity log logs are significantly cheaper than contract storage. When a user likes a post in Socio3, we emit a PostLiked event instead of storing the like inside a heavy on-chain array:
By indexing postId, liker, and author, we write their values to the transaction receipt's log topics. This makes it possible for external nodes to index and filter these logs instantly.
2. The Asynchronous Pipelines
Once the transaction is included in a block, the event log becomes permanent on the Polygon Amoy network. Two separate indexers listen to these logs:
Pipeline A: The Feed Indexer (The Graph)
- The Graph Node processes block receipts.
- It detects the
PostLikedevent. - The AssemblyScript handler increments the
likeCountentity in the subgraph cache. - When other users request the feed, they see the updated like count instantly via GraphQL.
Pipeline B: The Notification Daemon (Express Backend)
- A background daemon (such as a Web3 websocket listener or a Graph webhook) detects the event log.
- It reads the
authorandlikerparameters. - The Node.js server writes a record to Firestore:
SmartAccount.soltext
- A Firestore onSnapshot listener triggers a UI push notification on User B's screen.
3. Decoupling Web2 and Web3 State
Notice the separation of concerns:
- Blockchain: Cryptographic ledger of truth (authenticates the transaction, prevents double-liking).
- The Graph: Read cache for pagination, filters, and rendering.
- Firebase: Dynamic, short-lived notification delivery.
If the notification database crashes, the core social graph remains completely unaffected on-chain. If the indexer node goes down, the client can fall back to direct contract calls temporarily.
Do not write state variables for temporary features (like 'last active time' or 'like counts') directly to Solidity variables if they are not needed for smart contract execution logic. Emit events instead. Storage costs gas; event logs are cheap.
In a Web3 marketplace, a buyer places a bid. Design the event-driven notifications pipeline.
- What event does the contract emit?
- Which parameters should be indexed (topics)?
- Sketch the pipeline that alerts the seller's browser without polling the RPC node.
Was this lesson helpful?
Let us know what you think of this specification. (submitting anonymously)
