r/FastAPI • u/olzhas89 • Oct 13 '24
feedback request I've built real-time chess with FastAPI
Hi r/FastAPI,
I was looking for a fun weekend hacking project and decided to build a chess game with FastAPI.
The project was a lot of fun to build, especially the game communication logic.
Sharing here for anyone interested:
Live demo:
NOTE: You need another player online. If the wait is too long and you just want to play alone like a psycho explore the game, you could open two browser windows, or use two machines / devices.
Source code:
https://github.com/olzhasar/pyws-chess
Cheers
93
Upvotes
View all comments
Show parent comment
2
u/olzhas89 Oct 14 '24
Thank you!
I believe one still needs to use httpx.AsyncClient for that, but it works quite well for testing http endpoints in my experience. AsyncClient accepts asgi app as an instance and handles everything in the same loop in which your tests run.
But as with testing websockets-related stuff, it's hard though. AsyncClient does not support websockets out of the box, whereas built-in TestClient spawns a different asyncio loop in a separate thread. Therefore, if you have some state that you need to test, you're basically out of luck. There is a relevant issue opened in the Starlette repo, but AFAIK it's been hanging for a while, cause it requires a huge client redesign I assume.