Hacker News new | ask | show | jobs
Implementing Stockfish that can run in the browser of my Next JavaScript App?
1 points by waterdolphin 680 days ago
I posted on Reddit about this and based on a suggestion there I am posting about it here.

I have an idea for a simple free chess site that I want to implement in Next JS for practice and to build something that I enjoy. As part of this I need to figure out how to get a version of Stockfish working within my repo. I am using NextJS and my project is hosted through Vercel.

I found a Stockfish endpoint but that seems to be going down sometimes and the latency is not reliable for that either. I did a bit of googling and found that a WASM version of Stockfish can run in the user's browser but that seems to need an emscripten compiler to compile it. This is not something that I'm familiar with at all and I've spent the past day just talking with GPT, Claude and just searching the internet and find myself completely stuck. Can someone who has gone through this process and has it working walk me through the steps I need to take?

2 comments

The lazy way is download lichess's stockfish wasm bundle and import it into your project.
Could you elaborate on this? I don't see where the stockfish wasm bundle is there in any of the lichess repos?
Its javascript so you should be able to just use network inspector tab in chrome. I did try briefly and didn't see it, but it really should be in there somewhere.

There's also https://www.npmjs.com/package/stockfish.wasm https://github.com/lichess-org/stockfish.wasm

If you're still stuck and need to get emscripten working -- I've had success using WSL to compile emscripten on windows, so if you go that route its just a bunch of command-line calls.

You're on the right path. There's a bit of a learning curve for what you want to do but it should be doable.

Stockfish is written in C++. What you need to do is compile the C++ to either to WASM or Javascript -- a browser hostable environment.

Emscripten is a compiler toolchain for turning regular C and C++ programs into WASM or JS suitable to be embedded into a web browser. Stockfish doesn't include a GUI or anything, so you'll need to write or adapt a browser-based UCI (Universal chess interface) client, as well, and connect that to the Stockfish engine thread.