Hear me out. IN THEORY, Streamlit is super nice for multiplayer gaming. Each player has its own thread and they share a Python process. This makes it incredibly easy to develop any sort of player2player interactions.
Of course, there will be performance limitations. This is Python we are talking about after all.
So, I’ve decided to give it a try and implement a multiplayer snake game (App, GitHub):
It works very nice locally, but at Streamlit Cloud it cannot consistently pull even 8 FPS. The latency is unstable and some frames take 2-3 times longer to generate.
I use st.markdown with image bytes embedded to generate a frame, each frame is around 768x768 pixels (st.image worked horribly for this, I think it tried to cache each frame).
Do you have any ideas what can I try to make the game playable? I’m open to any ideas and feel free to make any changes to the repo yourself!
Currently I am thinking of:
Custom component that generates the frame based on the game state (basically just transfer non-zero pixels)
Played around with caching and image resizing at client, and I think it is more or less playable now! There are still hiccups sometimes, but not as bad as it used to be. I think it is as reliable and responsive as it can be with Streamlit.
Thank you! 8 fps is the tick rate of the game, so if it is maintained for you, this is perfect. Perhaps the latency and stability issues with Streamlit Cloud for me are related to how far I am from the server. I am in EU and I’d imagine the Cloud is hosted in the US.
I’ve updated the game with streamlit-webrtc display, and I think it is now as responsive as it can be. The latency is still noticable for me, but not sure if I can do anything about it.
This WebRTC component is a must-have if you are building anything close to real-time, go check it out!
Very nice, I can definitely feel it being smoother with this update. This isn’t a use-case I’ve considered, but I’m glad to have learned this, perhaps someday I’ll need this technique for something.