What are Server-Sent Events (SSE)?
Server-Sent Events (SSE) is a simple, efficient protocol for receiving real-time updates from a server over HTTP. Think of it as a one-way pipe where the server continuously pushes data to your client as events happen.
How SSE Works
SSE uses a standard HTTP connection that stays open, allowing the server to send multiple messages over time:
Unlike traditional HTTP requests where you ask for data and get a response, SSE keeps the connection alive and lets the server push updates whenever something happens — no polling required.
Why SSE for Blockchain Streams?
SSE is the perfect fit for real-time blockchain data:
Built on HTTP/HTTPS
- Works with existing infrastructure (load balancers, proxies, CDNs)
- No special ports or protocols needed
- Benefits from standard HTTP security (TLS, authentication headers)
Simple Client Implementation
- Native browser support via
EventSourceAPI - Minimal code to get started — just a few lines
- Automatic reconnection built into the browser API
Efficient for One-Way Data
- Server pushes updates as they happen
- Lower latency than polling
- No overhead of establishing new connections for each update
Firewall-Friendly
- Uses standard HTTP ports (80/443)
- Works through corporate firewalls and proxies
- No special network configuration needed
SSE vs Other Technologies
SSE vs WebSockets
WebSockets provide bidirectional communication, but that's overkill for blockchain event streams where you only need the server to send data.
| Feature | SSE | WebSockets |
|---|---|---|
| Direction | One-way (server → client) | Two-way (server ↔ client) |
| Protocol | HTTP/HTTPS | Custom ws:// protocol |
| Complexity | Simple | More complex |
| Browser support | Native EventSource | Native WebSocket |
| Automatic reconnection | Yes (built-in) | No (manual) |
| Use case | Real-time updates | Real-time chat, gaming |
For blockchain event streams, SSE is simpler and more efficient since you rarely need to send data back to the server.
SSE vs AMQP (RabbitMQ, etc.)
AMQP (Advanced Message Queuing Protocol) is powerful but adds significant operational complexity.
| Feature | SSE | AMQP |
|---|---|---|
| Infrastructure | Just HTTP server | Message broker required |
| Client libraries | Native browser support | Custom client libraries |
| Setup complexity | None (HTTP endpoint) | Broker deployment & management |
| Authentication | HTTP headers | AMQP credentials |
| Firewall compatibility | Excellent | Often blocked |
| Scalability | Horizontal (standard HTTP) | Vertical (broker limits) |
AMQP is designed for complex message routing between services. For simply receiving blockchain events in your app, SSE is dramatically simpler:
With SSE (Surflux):
const eventSource = new EventSource(
'https://api.surflux.dev/events?api-key=abc123'
);
eventSource.onmessage = (e) => console.log(e.data);
With AMQP:
- Deploy and manage RabbitMQ/Kafka cluster
- Configure queues, exchanges, bindings
- Handle authentication and connection management
- Install and maintain client libraries
- Deal with queue overflow and message persistence
- Monitor broker health and performance
SSE vs Polling
Polling is the old way — repeatedly asking "any updates?" every few seconds.
| Feature | SSE | Polling |
|---|---|---|
| Latency | Instant (push) | Delayed (poll interval) |
| Server load | Low | High (constant requests) |
| Bandwidth | Efficient | Wasteful (empty responses) |
| Scalability | Excellent | Poor (request overhead) |
| Implementation | Simple | Simple |
With polling, you're making thousands of requests and getting "no updates" most of the time. SSE eliminates this waste entirely.
SSE in Practice
Basic Usage
const eventSource = new EventSource(
'https://flux.surflux.dev/events?api-key=your_api_key'
);
eventSource.onmessage = (event) => {
const data = JSON.parse(event.data);
console.log('Received:', data);
};
eventSource.onerror = (error) => {
console.error('Connection error:', error);
};
Automatic Reconnection
If the connection drops, the browser automatically reconnects and resumes from the last event:
eventSource.onopen = () => {
console.log('Connected');
};
eventSource.onerror = () => {
// Browser automatically reconnects
console.log('Connection lost, reconnecting...');
};
Resume from Last Event
SSE supports event IDs, allowing you to resume exactly where you left off:
const lastId = localStorage.getItem('lastEventId');
const url = `https://flux.surflux.dev/events?api-key=key&last-id=${lastId || '$'}`;
const eventSource = new EventSource(url);
eventSource.onmessage = (event) => {
// Persist the event ID
localStorage.setItem('lastEventId', event.lastEventId);
// Process the event
const data = JSON.parse(event.data);
handleEvent(data);
};
Why Surflux Uses SSE
We chose SSE for Surflux Flux Streams because:
Developer Experience Getting started takes literally 5 lines of code. No complex setup, no message brokers, no special libraries.
Production Ready SSE scales horizontally like any HTTP service. Add more servers behind a load balancer and you're done.
Reliability Automatic reconnection and event ID resumption mean you never miss an event, even if your connection drops.
Universal Compatibility Works in browsers, Node.js, mobile apps, serverless functions — anywhere that can make HTTP requests.
Cost Effective No separate infrastructure to deploy or manage. No message broker licenses or operational overhead.
Browser Support
SSE is supported in all modern browsers via the native EventSource API:
- Chrome/Edge: Full support
- Firefox: Full support
- Safari: Full support
- Node.js: Use libraries like
eventsourceorfetchwith streaming
For older browsers or environments without native support, polyfills and libraries are widely available.
Next Steps
Ready to start receiving real-time blockchain data? Check out:
- Package Events — Subscribe to smart contract events
- Address Events — Monitor wallet activity
- Object Changes — Track object state changes