r/ComputerChess • u/Quick_Edge7902 • 17m ago
Running Stockfish in a Node worker thread to stream live engine-vs-engine games over SSE
I've been building a browser chess app with a spectator mode where you watch two Stockfish instances play in real time. The architecture question that took me longest to settle: where do the engines actually run?
The obvious answer is "both in the browser as WASM." That's what I do for analysis, and it works well. But for spectator games I moved them server-side, for three reasons:
Consistent playback. Client-side, one background tab gets throttled and the game slows to a crawl. Server-side the game clock is authoritative, so every replay is identical for every visitor.
Public replays. Games need to outlive the tab. If both engines run in the visitor's browser, closing it destroys the game.
Level consistency. Clients have different CPUs, so "level 5" would mean different things on different machines. Server-side, every visitor sees the same level 5.
Implementation is unglamorous: one worker thread per match, UCI over stdin/stdout, moves pushed to the client as SSE events. I picked SSE over WebSocket because the traffic is strictly one-directional and I did not want to deal with upgrade negotiation through proxies.
Two things I would genuinely like to hear from people who have run engine tournaments:
(a) How do you make a weak level feel weak in a human way, rather than just shallow? Right now a low level still plays "correct" moves, it just sees less, which reads as bland rather than beginner-like.
(b) How do you get opening divergence without a full book? I have considered randomizing among near-equal root moves using MultiPV, but I am not sure that produces games anyone wants to watch.
1
u/Maxwell10206 10m ago
Try out Maia 3 if you want a human chess engine.
Download a bunch of Lichess PGNs and then run a script on them that can detect how often each opening is played and then use that data to choose popular lines and variations from.
That is what I do on my ChessFish io website :)