I am creating a chess app with a front-end written in React. For position evaluation, I want to have Stockfish set up on my web hosts server. But, I have no idea where to start! Are there any good tutorials on how to do this?
I need help with:
1. How to setup Stockfish on the server.
2. How to connect to the Stockfish evaluation function via my React front-end.
Are there any tutorials on how to do this?
Setting up Stockfish on a server
Moderator: Ras
-
- Posts: 28321
- Joined: Fri Mar 10, 2006 10:06 am
- Location: Amsterdam
- Full name: H G Muller
Re: Setting up Stockfish on a server
I always use my utility connect.exe to set up engine servers (on Windows). It works both as a server and a client. When you start it as a server (with an engine specified on the command line), it starts waiting for an incoming connection on the specified (or default) port. And after the connecting client gives the proper password as the first line, it starts the engine, and all other commands go directly to the engine. And the engine output is sent back over the TCP/IP connection to the client.
-
- Posts: 775
- Joined: Sat Sep 08, 2018 5:37 pm
- Location: Ukraine
- Full name: Maksim Korzh
Re: Setting up Stockfish on a server
If you have python on your back-end python-chess might be very helpful:Jon12345 wrote: ↑Mon Mar 29, 2021 1:37 pm I am creating a chess app with a front-end written in React. For position evaluation, I want to have Stockfish set up on my web hosts server. But, I have no idea where to start! Are there any good tutorials on how to do this?
I need help with:
1. How to setup Stockfish on the server.
2. How to connect to the Stockfish evaluation function via my React front-end.
Are there any tutorials on how to do this?
https://python-chess.readthedocs.io/en/ ... ngine.html
It implements UCI/Xboard protocols and you can use it as the middleware to run SF as a OS process and than interact with it via stdin/stdout.
However I wouldn't go for setting SF on back end. Imagine 100 concurrent users making HTTP requests asking SF to evaluate position,
what would happen to your server? Well, you can limit the search depth etc, but the alternative is to run stockfish on the front end as the part
of your react app. In this case all the calculations are done within the user's browser. This is how lichess and chess.com work. Lichess uses WASM version of SF and chess.com uses WASM of specifically tuned Komodo (If I'm not mistaken), in both cases engine works on front end,
WASM port of SF is an option but it doesn't support all the browsers but only Chrome and Firefox (as far as I'm aware from their readme),
so using JS port of stockfish might be on the cards. Alternatively you can use tomitank engine it's somewhere around 2900 Elo and written in pure JS.
Also it's possible to use SF NNUE via JS, well at least they do it in SF eval quide (but user needs to select local NNUE which might be a bit of a pain).
To do it how you want you'll need:
1. UCI protocol implemented on your back end
2. Async engine communication between your backend and SF process via stdin/stdout
3. API that would be outputting scores extracted from stdout and redirecting them to your react app.
I did something similar: https://maksimkorzh.pythonanywhere.com/
It's not SF but SF NNUE is used (some old one)
I made some tutorials on this:
Hope it gives some clue)
Didactic chess engines:
https://www.chessprogramming.org/Maksim_Korzh
Chess programming YouTube channel:
https://www.youtube.com/channel/UCB9-pr ... KKqDgXhsMQ
https://www.chessprogramming.org/Maksim_Korzh
Chess programming YouTube channel:
https://www.youtube.com/channel/UCB9-pr ... KKqDgXhsMQ
-
- Posts: 80
- Joined: Tue May 11, 2010 6:18 pm
Re: Setting up Stockfish on a server
Thanks for all that detail. Very handy. I am not using Python as my programming languages are React and Javascript. My hosting is a Linux server so Windows is out of the question. I cannot run Stockfish on someones browser since it has a GPLv3 license and that would require me to release all my source code and effectively make my app open-source. So, it has to run on the server, not in the browser.
It looks like I need to learn how to do the UCI protocol on my backend. Currently, I don't have a backend. I've only created the front-end. I am not sure if I somehow install Stockfish on the server and then have some other code running on the server to communicate as some kind of UCI API. All new to me. I will check out some of those tutorials that @maksimKorzh kindly linked to. Hopefully it will give me some direction.
This is all a big hurdle to me and I am also unaware how much strain concurrent users would put on the hosting, with all those little stockfish evaluations going on at once! And my programming abilities are straining at the limits!
It looks like I need to learn how to do the UCI protocol on my backend. Currently, I don't have a backend. I've only created the front-end. I am not sure if I somehow install Stockfish on the server and then have some other code running on the server to communicate as some kind of UCI API. All new to me. I will check out some of those tutorials that @maksimKorzh kindly linked to. Hopefully it will give me some direction.
This is all a big hurdle to me and I am also unaware how much strain concurrent users would put on the hosting, with all those little stockfish evaluations going on at once! And my programming abilities are straining at the limits!
Jon
-
- Posts: 28321
- Joined: Fri Mar 10, 2006 10:06 am
- Location: Amsterdam
- Full name: H G Muller
Re: Setting up Stockfish on a server
Well, connect.exe is also available as Linux binary (simply called 'connect' then). But I don't think it allows multiple connections. (Although it should be easy to alter it such that it does; it must start up a separate process for running the engine anyway; it might as well start up multiple processes.)
But you have to make your client communicate with the server somehow, and you might as well use UCI to do that. This is what connect.exe assumes. (Well, it actually assumes you communicate with it through TCP/IP as the engine would communicate on the command line. So it only communicates in UCI if the engine is a UCI engine.) It doesn't do any conversion of what goes to or comes from the engine, it just relays it. Only the first line of the input (the handshake between client and server) does not go to the engine, but is used to decide whether the connection is accepted, or closed immediately.
But you have to make your client communicate with the server somehow, and you might as well use UCI to do that. This is what connect.exe assumes. (Well, it actually assumes you communicate with it through TCP/IP as the engine would communicate on the command line. So it only communicates in UCI if the engine is a UCI engine.) It doesn't do any conversion of what goes to or comes from the engine, it just relays it. Only the first line of the input (the handshake between client and server) does not go to the engine, but is used to decide whether the connection is accepted, or closed immediately.
-
- Posts: 80
- Joined: Tue May 11, 2010 6:18 pm
Re: Setting up Stockfish on a server
I sense this is all going to be a lot more complicated than I hoped. Looks like I will have to chip away at it slowly, inching forwards at a snails pace. Maybe there is someone here who has tried it using a JavaScript or React front-end who can add to the discussion.
My app feels kinda useless without an engines evaluation, so it is a critical piece of the puzzle. I'm always impressed when you guys figure this stuff out from scratch!
This discussion might also help others create chess apps using Stockfish. Overcoming technical hurdles can become barriers to innovation. Stockfish itself stands on the shoulders of other engines. I find the whole history of chess development fascinating and these tiny 1K to 2K engines out there, they play decent chess!
My app feels kinda useless without an engines evaluation, so it is a critical piece of the puzzle. I'm always impressed when you guys figure this stuff out from scratch!
This discussion might also help others create chess apps using Stockfish. Overcoming technical hurdles can become barriers to innovation. Stockfish itself stands on the shoulders of other engines. I find the whole history of chess development fascinating and these tiny 1K to 2K engines out there, they play decent chess!
Jon
-
- Posts: 28321
- Joined: Fri Mar 10, 2006 10:06 am
- Location: Amsterdam
- Full name: H G Muller
Re: Setting up Stockfish on a server
Well, a good approach might be to just start with running 'connect' and stockfish on your server machine. Then you don't have to worry about that end, and can concentrate on your app making the TCP/IP connection, and conducting the dialog necessary to make Stockfish do what you want it to do. That 'connect' might only be single user makes no difference at that point, as you would be the only user. After you are sure that your app works, you can start working on improving the server side.
-
- Posts: 80
- Joined: Tue May 11, 2010 6:18 pm
Re: Setting up Stockfish on a server
Yes, good place to start. I could go to the Stockfish Discord server to ask about how to install Stockfish on my server. Maybe its just a case of doing some kind of npm install and upload to a folder, I have no idea. I only need to do it once and then I will know forever!
Then I can look at building some kind of connection between my front end and Stockfish.
Finally, try to find out how to turn Stockfish into a multi-user type thing.
Then I can look at building some kind of connection between my front end and Stockfish.
Finally, try to find out how to turn Stockfish into a multi-user type thing.
Jon
-
- Posts: 28321
- Joined: Fri Mar 10, 2006 10:06 am
- Location: Amsterdam
- Full name: H G Muller
Re: Setting up Stockfish on a server
If your server runs Linux, your distro probably has a package manager (like apt-get for Ubuntu), and you can simply install the binary stockfish package from the distro repository ("sudo apt-get install stockfish").
And it is not Stockfish that has to be turned into a multiuser thing; you will simply start one Stockfish for each user that connects. This can be done for any program, without special modifications.
The issue is that the server program you run for this must accept multiple simultaneous connections from remote clients.
And it is not Stockfish that has to be turned into a multiuser thing; you will simply start one Stockfish for each user that connects. This can be done for any program, without special modifications.
The issue is that the server program you run for this must accept multiple simultaneous connections from remote clients.
-
- Posts: 300
- Joined: Mon Apr 30, 2018 11:51 pm
Re: Setting up Stockfish on a server
Do note that Stockfish is not robust against malicious input, so if you're allowing untrusted FENs (or worse, untrusted UCI), you're at the risk of opening up a security hole.