Getting data from the blockchain can be difficult given that blockchain transactions are included asynchronously and that state is only eventually consistent.
There are three key methods to get near real-time data updates from the blockchain: Polling, WebSockets, and Webhooks. Why would you want data updates for the blockchain? Examples include: when you want to send an email when DAO voting is complete, update your on-screen hit points in a role playing game, or get a notification when your transfer is complete, etc.
In this article, we will show how these three methods work and how you can start using them today to monitor on-chain state changes.
What is MultiBaas? Designed to streamline the integration of blockchain into applications, MultiBaas is a SaaS platform that provides developers with a suite of tools to develop DApps as well as monitor and respond to events on the blockchain.
Polling for state changes
Polling, a traditional method of event monitoring, involves regularly querying the blockchain for updates. While simple to implement, polling comes with latency and resource consumption tradeoffs.
Here's how polling typically works for a decentralized application:
⏱️ The application or client sets a specific time interval or schedule at which it will query the blockchain network for updates.
📞 At each polling interval, the application sends a request to the blockchain network, typically through an API or SDK.
🔍 The API receives the query, processes it, and returns the data as a response to the client.
✉️ The response contains the requested data or information, such as transaction details, block information, or smart contract data.
✅ The application or client receives the response and handles the data. It may update its own systems, trigger actions, or display the information to users.
🔁 After processing the response, the application waits until the next polling interval and repeats the request to check for any new updates on the blockchain network.
While polling can be effective for obtaining data from the blockchain, it is not as efficient or timely as other mechanisms like WebSockets or Webhooks.
WebSockets: Efficient two-way communication
WebSocket is a communication protocol that provides full-duplex communication channels over a single TCP connection, allowing bi-directional communication between a client and a server. MultiBaas provides a WebSocket stream of smart contract events for a given address as an advanced feature.
Here is a general description of how WebSockets works with MultiBaas:
🔗 A WebSocket connection is established between the client and MultiBaas.
↔️ Once an event occurs on the blockchain, MultiBaas receives a notification about it. MultiBaas then pushes the data to the client application via the WebSocket connection.
✅ The client application can receive a continuous stream of blockchain data through the WebSocket connection with MultiBaas.
Instead of relying on periodic polling or frequent requests for updates, WebSocket connections allow for persistent connections and instant data transmission, making it ideal for live applications that require immediate updates and interactions with the blockchain such as chat applications, real-time collaboration tools, and live data streaming.
Alternatively, it is possible to monitor blockchain events using WebSockets by using a node provider like Infura and the EVM JSON RPC API over WebSockets. Here’s a walkthrough on how to use WebSocket Subscriptions with the Infura Polygon WSS API.
Some of the challenges with interacting with blockchain node websockets directly are:
- Manually decoding events
- Dealing with chain reorganizations
- Handling nodes going away or falling behind
Note that MultiBaas handles this complexity for you automatically.
Webhooks
Webhooks send a notification about a blockchain event occurring to a configurable HTTP endpoint. They work on a simple principle: when a specific event occurs on the blockchain, a webhook is triggered. This webhook then sends a notification to a specified HTTP endpoint or URL, typically an application or server, providing details about the event in near real-time.
Here is a high-level overview of how MultiBaas webhooks work:
🔔 A specific event takes place on the blockchain network, such as a new transaction being added to a block or a smart contract execution.
🪝A webhook provider such as MultiBaas detects the event and triggers the corresponding webhook.
📦 The webhook generates a payload containing relevant information about the event, such as transaction details, block information, or smart contract data.
📩 The webhook sends an HTTP POST request to the designated endpoint or URL, delivering the payload with the event details.
✅ The receiving application or server at the endpoint processes the incoming request and extracts the payload data. The application can now use the received data to update its own systems, trigger actions, or notify users.
MultiBaas further abstracts and simplifies the process of getting the data because it has logic built in to handle blockchain event deduplication, handle stateful management of blockchain transactions, perform automatic retry with backoff, and more.
Webhooks find practical application in crypto asset trading on exchanges. For example, they can be used to enable push notifications to inform users about asset transactions such as purchases or sales. In ecommerce, webhooks allow platforms like Shopify to send notifications when a transaction occurs.
Summary
Which one is the best for your application?
It depends on the use-case.
If you need live, instant updates for building a chat application, a real-time collaboration tool, or for live data streaming, then WebSocket streaming is what you need.
But if you’re building a dashboard to monitor token prices or you don’t need an open connection then you might use polling.
If you need to establish a system so your user can receive push notifications about on-chain events, then you may want to use webhooks.
Get started with MultiBaas
You can create a free production account on MultiBaas to try out these techniques by signing up here. For further information, see our webhooks documentation. WebSockets require a custom configuration so please contact us if you would like to make use of WebSockets with MultiBaas. For more general information on programming with WebSockets, click here.