Understanding SSE for Real-Time Sync
When we built the Universal Clipboard, we needed a way to push data from one device to another in real-time. We chose Server-Sent Events (SSE) over WebSockets. Here is why.
The Real-Time Challenge
Standard HTTP requests are unidirectional: the client asks, the server answers. To create a "sync" effect, most apps use one of three techniques:
- Polling: Client asks "any new data?" every 5 seconds. (Wasteful)
- WebSockets: Full-duplex bidirectional communication. (Complex to scale)
- Server-Sent Events: One-way data stream from server to client. (The "Sweet Spot")
How Universal Clipboard Uses SSE
When you open a room in Universal Clipboard, your browser establishes a persistent connection to our relay server using the EventSource API.
const eventSource = new EventSource('/api/relay?room=YOUR_ROOM_ID');
eventSource.onmessage = (event) => {
const newClip = JSON.parse(event.data);
updateClipboard(newClip);
};Why SSE is Superior for This Use Case
Scaling with SSE
Because SSE is lightweight, our relay servers can handle thousands of concurrent "listening" devices with minimal CPU overhead. When you "Submit" a new clip, we use a standard POST request to the server, which then broadcasts that data through the active SSE streams to all other devices in the room.