r/ComputerChess • • 5d 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:

  1. 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.
  2. Public replays. Games need to outlive the tab. If both engines run in the visitor's browser, closing it destroys the game.
  3. 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.

0 Upvotes

3 comments sorted by

1

u/Maxwell10206 5d 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 :)

2

u/Quick_Edge7902 5d ago

Maia-3 is a much better answer than what I was doing. Reduced-node Stockfish always felt shallow rather than weak — it still plays the correct move, it just sees less. A policy trained on human games fixes that at the source. I hadn't realised it covers 600–2600 now.

On the PGN mining: my concern is that one frequency table across all ratings gives you an "average player" that nobody actually plays like, so I assume you stratify by rating band and keep a floor on the tail?

Thanks for the pointer — I'll have a look at ChessFish.

1

u/Maxwell10206 5d ago

I just added a simple temperature knob for opening variety. Lower the temperature and you get your typical stronger more popular book lines, increase the temperature and you get more crazy rare openings.

Separating openings by elo bands is fine too, but I felt the temperature knob was more fun and useful for my website. But yeah many ways to go about it once you got the Lichess games processed :D !