Real-time Chat Game Server

Real-time chat and real-time game servers are nothing new, they've been around for years, decades. However they have generated a lot of buzz recently, and associated buzz word technologies. Nowadays we'll hear of the latest and greatest web platforms like Node.js, React, Google Firebase, Amazon Gamelift, Chatbots etc offering such capabilities. But truth be told, it is possible to achieve this with plain and simple Javascript and PHP. And when I say PHP I'm talking about simple standard PHP that is available from most cheap web hosting companies, not specialist PHP installations set up for WebSockets.

This technology is called HTTP Polling, more specifically HTTP Short Polling and HTTP Long Polling, achieved with simple browser-side Javascript, standard HTTP Post or Get requests, and simple server-side PHP. In fact these approaches are similar to what Facebook originally used when its Facebook Chat/Messenger was introduced in 2008. This approach is still viable and a nice simple solution today for simple chat and realtime gaming server requirements, e.g. text based multi-user chat (not audio/video chat), and multi-player position state tracking in a game server. And what is more, it's available to the masses, to projects, hobbyists and companies that might only have standard PHP web server hosting.

If you're thinking "surely PHP isn't up to coping with this kind of rapid client/server request strain?", think again about how those search as you type autocomplete text field/dropdown UI components work on most webites, UI components which strain the server far more than simple chat real-time gaming requirements would.

Below is a simple basic working example. Open up multiple browser tabs/windows of this page on your desktop and/or mobile device to have a chat with yourself or others. Chat logs are reset on the hour.

The client-side code on this page can be View Source'd. The PHP code for awrealtimeserver.php is (it requires a sub-folder called log):

This is very basic code, definitely not production worthy with little error checking or optimisation, but it is presented in this simple form for easier understanding and learning purposes, even though this is not a tutorial!

Moving forward, this approach could be used in VR projects (multiple users represented as avatars in a VR world, e.g. audience for VR Music Videos), and for web games.