Share intent, not events
Keeping two browsers playing the same song at the same moment looks like it needs a real-time server. It does not. What it needs is a smaller definition of what the two screens actually have to agree on.
I wanted two browsers in different cities to play the same song at the same moment, stay in step when either person paused or skipped, and carry a chat alongside. This is the Saath Saath mode in Samohit Studio, a music player I built with Rohit.
The instinct is to reach for a real time server. Sockets, an event stream, one screen emits a pause and the other receives it. That is how these things are usually drawn, and it is the version I assumed I would build.
It is also the expensive version, and working out why it was expensive turned out to be the whole design.
Events are a worse representation than state
An event stream says what happened. Play was pressed. Seek to 0:42. Next track.
That sounds natural and it quietly creates a lot of work. Events must arrive, in order, exactly once. Miss one and the two screens diverge with no way to notice. Reconnect after a dropped connection and you need a replay protocol, or a resync, or both. Every event is a chance to be wrong, and the wrongness accumulates silently.
So I asked what the two screens actually have to agree on, and the answer was much smaller than the event stream suggested. Four things:
- which song
- whether it is playing
- what position it was at
- the server time that position was recorded
That is the entire shared record. Not what happened, just what should currently be true.
Each screen reads it and works out the rest. Expected position is the stored position plus the time elapsed since it was stored. If its own player has drifted more than about two seconds from that, it quietly seeks to catch up. No jump the listener notices, no argument between the two players.
The property that matters: the state is idempotent and self-correcting. Read it twice, get the same answer. Miss a read entirely and the next one still lands you in the right place. A dropped connection needs no recovery logic, because there is nothing to recover. You just read the current state, same as always.
Once the shared record is small, almost all the hard parts of a real time system stop existing.
And then polling is enough. Every couple of seconds, backing off to eight when the tab is in the background, with a version number on the record so nothing downloads unless something actually changed. No socket server to keep alive, no reconnection handling, no sticky sessions.
Whose clock
The first thing to break was time itself.
"The position was 0:42 at timestamp T" is only useful if both machines agree on what T means. They do not. A phone whose clock is a minute off computes an expected position a minute wrong and seeks confidently to the wrong place.
The fix is to never trust the local clock as an absolute. Each screen measures the gap between its own clock and the server's from the round trip of every request it already makes, and corrects for it. The data is free, you are making the request anyway.
I would generalize this. Any time a design says "and the timestamp," ask whose clock, and whether you have ever checked.
Two screens, one intention
Two genuine races showed up, and both are about simultaneity rather than scale. They would look identical with a hundred people in the room.
Both screens ask for the next song. A track ends. Both clients notice within milliseconds of each other and both politely request the next one. The playlist advances twice and now you are a song ahead of where anyone wanted to be.
The fix is an idempotency key. A once only key tied to the version of the room that requested the skip, so the first request wins and the second is recognized as a duplicate of the same intention rather than a new one. The important part is that the key is scoped to that transition, not to a time window. A time window would have been a guess about how fast the second request arrives, and guesses about timing are how you get a bug that only appears on a bad connection.
A screen hears its own echo. You press pause. The server records it. Your next poll hands that pause back and your client dutifully applies it. Harmless here, but the same shape in a noisier system is an oscillation. A screen ignores the update that its own action caused.
I am being honest about an edge I have not fully closed. If both people act on the same control within one poll window, one of them simply wins, and I have not needed anything more careful than that. With a small room and a two second tolerance it has never been visible. I would not ship that reasoning into a system with more people in it without thinking harder.
Backpressure, cheaply
The chat is the only place either person can write unbounded data into the shared record, so it is the only place that needs limiting. Messages are capped at 300 characters and three per second per person, and anything that looks like markup is stripped before it is stored.
None of that is clever. It is just noticing that one surface is different in kind from the others and putting a bound on it before someone finds out.
Test with two of them or do not bother
Every bug above is invisible to a single client. A test that opens one browser, presses play and checks that the song is playing passes against a product that is broken in the only way that matters.
So the tests drive two real browser windows at once and assert that play, pause, skip, seek and chat stay in step between them. That suite is slower and more annoying than a unit test and it is the only one that was ever going to catch the double skip.
The rule I took from it: test with the client you actually ship, in the configuration the product is for. If the product is two screens, one screen is not a test.
The part that generalizes
Share state, not events, and make the shared state as small as you can bear. Let each client derive what it can derive locally. Give every write an identity so a repeat is recognizable as the same intention. Never trust a clock you have not measured.
Do that and a problem that looked like it needed infrastructure turns out to need a record with four fields in it.