How I faked a real-time opponent on shared hosting with zero background processes
Shared hosting has one rule that quietly kills most multiplayer designs: you cannot run a persistent process. No WebSocket server, no daemon, no long-lived worker. Your PHP is born when a request arrives and dies when the response is sent. I ran into this building a head-to-head word game — two players racing to guess the same five-letter word. The multiplayer part was fine. The problem was what…
Faking a real-time opponent on shared hosting can be achieved without running a persistent process, as demonstrated by a head-to-head word game. The key to this solution is realizing that the bot does not need to exist between requests. Instead, its actions can be computed on the fly, based on the battle ID and elapsed time. This turns the bot into a pure function, eliminating the need for storage, scheduling, and race conditions.
The randomness in the bot's decisions is controlled by seeding it from the battle's ID. Separate seeds are used for different aspects of the bot's behavior, such as guess timing, word selection, and chat messages. This ensures that the same battle always produces the same bot output, regardless of how many times it's evaluated.
The bot's outcome is determined upfront, based on the desired difficulty level. This allows for a more controlled and tunable experience, rather than relying on an AI solver that may produce inconsistent results. By pre-computing the bot's guesses, the game avoids the unpredictability of a real solver, ensuring a fair and consistent experience for the players.
Polling the server for the bot's status is acceptable in this case, as the number of state changes is limited to a few over roughly two minutes. The update rate is low, making WebSockets unnecessary despite their reputation for providing low-latency updates. By matching the transport to the update frequency rather than the desire for modern technology, the solution stays within the constraints of shared hosting.
The broader lesson from this experience is that seemingly restrictive constraints can often be overcome by finding alternative implementations that are simpler and more stateless than the original idea. In this case, the deterministic function approach allowed for a working multiplayer game experience on shared hosting, despite the initial limitations imposed by the hosting environment.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.