You can monitor on-chain events on Solana through polling, WebSockets, Geyser, or Helius webhooks. Each method has its own advantages and disadvantages.
As a cryptocurrency developer, one of the most fundamental tasks is building systems that monitor the blockchain. You might be listening for payment confirmations, NFT sales, fund transfers, or simply monitoring accounts for security purposes. Regardless of the use case, it's crucial that the system you build for monitoring the chain is fault-tolerant, reliable, and optimized for latency.
In this article, we will explore several methods for building such systems on Solana, provide example use cases, and discuss how to use Helius to build a more robust solution.
Understanding On-Chain Event Monitoring
There are two primary ways to monitor on-chain events:
- Polling
- Streaming
Polling is a method where a client or application repeatedly checks a server or data source for new information. This can be done at set intervals or on demand. When a request is sent to the server, it responds with the latest data regardless of whether any changes have occurred. This means the client or application might repeatedly receive the same data even when there are no updates.
Streaming, on the other hand, is a technique where the server pushes data to the client or application when updates are available. This means the client doesn't need to repeatedly request data, and the server only sends information when changes occur. Since the server only sends relevant data, the transmission is more efficient and real-time.
While polling is a simple and widely used technique, it can result in high network traffic and may consume significant resources, especially when requests are made frequently. Streaming is generally more efficient because the server only sends updates, reducing both the amount of data transmitted and network traffic.
Streaming is typically used in applications where real-time data updates are critical, such as stock market movements, social media updates, and online gaming. Polling is more commonly used in applications where data updates are less frequent or less critical, such as email clients or news websites.
While both techniques have their advantages and disadvantages, streaming is generally the more efficient and preferred method for real-time data updates, while polling remains useful for certain applications where data updates are less frequent.
For most types of workflows on blockchain—especially Solana—you'll want to stick with streaming. There are several reasons for this, but streaming will be easier to implement and more capable of handling the high-throughput use cases that Solana excels at.
How to Monitor On-Chain Events on Solana
Solana is an extremely fast blockchain. It produces new blocks every 400 milliseconds, and these blocks typically contain thousands of transactions. Remarkably, this is actually the slowest it has ever been. As core engineers continue to develop the system, the chain's ability to process new data will only increase. Therefore, it's essential to design your system with scalability in mind.
Fortunately, you have several methods available for monitoring on-chain data on Solana:
- Polling
- WebSockets
- Geyser
- Helius Webhooks
Polling Method
This is the least preferred method for listening to events on Solana, although it might be conceptually the simplest.
Depending on the data you're trying to capture, you can simply set up a loop that repeatedly calls JSON-RPC methods on a Solana RPC (like Helius). Common use cases here include polling the "getBlock" method to listen for new blocks or polling "getSignaturesForAddress" to check for new transactions for a given address.
Interestingly, this might also be the best approach for very advanced use cases where you have highly custom logic for triggering new updates.
WebSockets Approach
Solana RPCs also expose PubSub WebSockets for developers to connect to. The available event types include:
- accountSubscribe: Subscribe to account updates
- accountUnsubscribe: Unsubscribe from account updates
- logsSubscribe: Subscribe to log updates
- logsUnsubscribe: Unsubscribe from log updates
- programSubscribe: Subscribe to program updates
- programUnsubscribe: Unsubscribe from program updates
- signatureSubscribe: Subscribe to signature updates
- signatureUnsubscribe: Unsubscribe from signature updates
- slotSubscribe: Subscribe to slot updates
- slotUnsubscribe: Unsubscribe from slot updates
Here's a small code example using JavaScript to listen for account changes through a Helius RPC (which supports WebSockets):
const solanaWeb3 = require('@solana/web3.js')
const connection = new solanaWeb3.Connection("https://rpc.helius.xyz?api-key=");
(async () => {
connection.onAccountChange(
new solanaWeb3.PublicKey("5yv6Vh8FNx93TXeSS94xy8VLZMbTqx4vXp7Zg5bDLZtE"),
(updatedAccountInfo, context) => console.log("Updated account info: ", updatedAccountInfo),"confirmed" );
})();One very important caveat about WebSockets is that while they're excellent for prototyping, we've found them to be quite fragile and unreliable in practice. We strongly recommend against using them for mission-critical workflows, as you might miss events.
Geyser Integration
TL;DR—You can have Solana nodes stream data directly to you through a plugin interface that customizes how you receive information. This is the fastest, lowest-latency method for streaming data on Solana and is absolutely necessary for DeFi liquidations and latency-sensitive applications.
Solana validators and RPCs have a unique Solana-native way of streaming data: Geyser plugins.
Validators have been enhanced to support a plugin mechanism called "Geyser" plugins, through which information about accounts, slots, blocks, and transactions can be streamed to external data stores such as relational databases, NoSQL databases, or Kafka. RPC services can then be developed to consume data from these external stores, allowing for more flexible and targeted optimizations like caching and indexing.
Unfortunately, setting up Geyser can be quite complex and expensive. Even after setup, you'll need significant DevOps work to ensure it runs smoothly. Fortunately, at Helius, we've developed something called GeyserVM that allows you to upload plugins and run them within seconds. Our high-availability and redundant clusters ensure you don't have to worry about any data issues, and since resources are shared, you can save over 200% monthly compared to running your own infrastructure!
Webhook Solutions
Webhook is a simple concept. In short, webhooks listen for events and send them to a server you set up when they occur. For example, you might be interested in sending notifications to your Discord server when NFT sales happen—webhooks can accomplish this seamlessly.
For most event listening workflows on Solana, webhooks will be the simplest, most flexible, and most cost-effective approach. The only scenario where webhooks might give you pause is for extremely low-latency use cases where a 5-millisecond difference could make or break your application, such as high-frequency trading.
Helius has built the most robust webhook product in all of cryptocurrency, allowing you to listen to up to 100,000 addresses, configure the types of events you want to monitor, and simply input your server's URL—that's all you need to get started.
One of the many benefits of such services is that you not only save weeks or months of developer time and effort, but you also get a resilient backend that scales as you grow.
Practical Use Cases
Now that you know how to listen to on-chain events on Solana, what should you do next? Here are some potentially interesting use cases:
Trading Bots:
- Trigger "NFT purchase" actions when NFTs are listed on Market X
- Initiate "liquidation" actions when margin positions become unhealthy
Monitoring and Alerts:
- Trigger PagerDuty integrations when programs emit specific logs
- Use Dialect to communicate warning actions when token account balances change by more than X%
Event-Driven Indexing:
- Send transactions directly to your database or backend when any transaction occurs on a given program
Notifications and Activity Tracking:
- Send Slack notifications or emails when transfers occur from Wallet X to Wallet Y
Analytics and Logging:
- Send events to ETL pipelines or keep them directly on Helius to view trends over time when Event X occurs
Workflow Automation:
- Trigger any set of operations when Event X occurs
👉 Explore real-time monitoring tools
Frequently Asked Questions
What is the main difference between polling and streaming for blockchain monitoring?
Polling involves repeatedly checking for new data at regular intervals, while streaming establishes a persistent connection that pushes data to your application as soon as it becomes available. Streaming is generally more efficient for real-time applications on fast blockchains like Solana.
Which method is best for listening to Solana events?
For most use cases, webhooks provide the best balance of simplicity, reliability, and cost-effectiveness. However, for ultra-low-latency requirements, Geyser plugins offer the fastest possible data access.
How many addresses can I monitor with Helius webhooks?
Helius webhooks allow you to monitor up to 100,000 different addresses, making them suitable for both small projects and enterprise-scale applications.
Are WebSockets reliable for production applications?
While WebSockets are excellent for prototyping and development, they can be fragile in production environments and may miss events. For mission-critical applications, webhooks or Geyser plugins are recommended.
What types of events can I listen for on Solana?
You can monitor various events including account changes, specific transactions, program interactions, log emissions, slot updates, and signature confirmations.
Do I need to run my own Solana validator to use Geyser?
No, services like Helius's GeyserVM allow you to use Geyser plugins without the complexity and expense of running your own validator infrastructure.
Final Thoughts
In this article, we've explored the differences between polling and streaming, discussed different methods for monitoring the Solana blockchain, and provided example use cases.
In summary, you can listen to on-chain events on Solana through polling, WebSockets, Geyser, or Helius webhooks. Each method has its advantages and disadvantages, so be sure to consider your system's requirements before deciding which approach to implement.