What Are WebSockets and Why Use Them?
WebSockets provide a persistent, real-time connection between your application and the Solana blockchain. Unlike traditional HTTP requests where you repeatedly ask “has anything changed?”, WebSockets let the blockchain notify you instantly when something happens.Real-Time Updates
Get notified instantly when accounts change, transactions occur, or blocks are produced
Efficient Resource Usage
One persistent connection instead of polling hundreds of HTTP requests
Low Latency
Sub-second response times for time-critical trading and monitoring applications
Standard Protocol
Uses Solana’s official WebSocket API - compatible with all Solana tooling
Core Concepts
Subscription Types
Account Subscriptions
Account Subscriptions
Monitor changes to specific accounts like wallet balances, token accounts, or program data.Use cases:
- Wallet balance tracking
- Token account monitoring
- Smart contract state changes
- NFT ownership updates
Program Subscriptions
Program Subscriptions
Watch all accounts owned by a specific program for any changes.Use cases:
- DEX trade monitoring
- DeFi protocol tracking
- NFT marketplace activity
- Gaming asset changes
Transaction Subscriptions
Transaction Subscriptions
Get notified when specific transactions are confirmed or when transactions mention certain accounts.Use cases:
- Payment confirmations
- Transaction status tracking
- Multi-signature approvals
- Contract execution monitoring
Slot Subscriptions
Slot Subscriptions
Monitor blockchain progression and finality at the slot level.Use cases:
- Block explorer applications
- Network health monitoring
- Consensus tracking
- Performance analytics
Commitment Levels
Understanding commitment levels is crucial for reliable applications:Fastest - Transaction processed by a validator but not confirmed
- Latency: ~400ms
- Risk: Can be dropped or reordered
- Use for: Real-time UI updates (with caution)
Want to learn more about commitment levels? Read our comprehensive blog post: Understanding Solana Commitment Levels
Available WebSocket Methods
The examples in this guide cover the most commonly used WebSocket methods, but Solana’s WebSocket API offers many more subscription types for specialized use cases.Complete API Reference
Explore all 18+ WebSocket methods. Each method includes detailed parameters, response formats, and examples.
Implementing Robust Reconnection Logic
WebSocket connections can disconnect for various reasons - network issues, server maintenance, or temporary outages. Production applications must implement reconnection logic to ensure reliability.Why Disconnections Happen
Network Issues
Network Issues
- Internet connectivity problems
- WiFi handoffs on mobile devices
- Corporate firewall timeouts
- ISP routing changes
Server-Side Events
Server-Side Events
- Planned maintenance windows
- Load balancer restarts
- RPC node updates
- Temporary overload protection
Client-Side Issues
Client-Side Issues
- Browser tab backgrounding
- Mobile app suspension
- Computer sleep/hibernation
- Process crashes or restarts
Detecting Disconnections
Reconnection Strategies
Testing Reconnection Logic
Critical for Production: Implementing proper reconnection logic is not optional for production applications. WebSocket connections will disconnect - plan for it, test it, and monitor it in production.
Troubleshooting
Connection Failed
Connection Failed
Symptoms: WebSocket fails to connect initiallySolutions:
- Verify your API key is correct and has sufficient credits
- Check the endpoint URL format:
wss://mainnet.helius-rpc.com?api-key=YOUR_KEY
- Ensure your firewall allows WebSocket connections on port 443
- Test with a simple ping first to verify basic connectivity
Frequent Disconnections
Frequent Disconnections
Symptoms: Connection drops every few minutesSolutions:
- Implement proper ping/pong heartbeat (shown in reconnection examples above)
- Check your network stability and corporate firewall settings
- Monitor your subscription count - too many can cause instability
- Verify you’re handling the connection lifecycle properly
Missing Messages
Missing Messages
Symptoms: Not receiving expected subscription updatesSolutions:
- Verify your subscription is confirmed (check the response)
- Ensure the account/program you’re monitoring has actual activity
- Monitor your connection state - missed messages often indicate disconnections
High Latency
High Latency
Symptoms: Slow message delivery, delays in updatesSolutions:
- Use “confirmed” instead of “finalized” commitment
- Reduce the number of active subscriptions
- Optimize your message processing logic
- Consider using multiple connections to distribute load
- Check your network connection quality
Memory Leaks
Memory Leaks
Symptoms: Application memory usage grows over timeSolutions:
- Implement proper subscription cleanup
- Remove event listeners when components unmount
- Clear message logs periodically
- Monitor subscription count and enforce limits
- Use weak references for callback functions where possible
Migration from Standard RPC
If you’re currently using HTTP polling, here’s how to migrate to WebSockets:Enhanced Capabilities
For applications requiring even more advanced features, consider using LaserStream gRPC:Historical Replay
Catch up on missed data when your application was offline
Multi-Node Aggregation
Enhanced reliability through data from multiple validator nodes
Higher Throughput
Handle more subscriptions and higher message rates
Enterprise Features
Advanced monitoring, analytics, and custom data pipelines
Ready to Get Started? Check out our WebSocket Quickstart Guide for practical examples and step-by-step implementation guides.