What you need is WebSockets
Rather than AJAX, where you send a request to the server and the server sends data back, web sockets allow the server to send data to any / all of the connected clients at any given time, and for the clients to messages to the server in the same fashion. This allows for full two-way communication between the client and the server without polling - once the connection is established, either can send information to the other whenever they want.
Note: if you just need your server to send a couple of messages to the client, but the client doesn't need to be able to respond, sockets might be an overkill (though they will definitely still get the job done). Check out server sent events instead.
Practically, you write some code which runs as a service in the background on your web server, clients connect to it using sockets, it keeps track of everyone currently connected, and can send / receive messages to any of them at any time (typically some sort of infinite update loop is used to check if anything has changed).
How do I write / run WebSockets?
Just because you write the rest of your backend in PHP doesn't mean your socket server has to be - it can be written in any language, though one with built-in socket support (like Java) is probably better.
If you run your server on *nix, and you want a really simple solution that works with any language, you might want to check out websocketd, which lets you use any language you want to write your websocket code without having to worry about any socket APIs.
It lets you use stdin
and stdout
for all the I/O, and then you can run it in the terminal using the websocketd
command, which creates a dæmon which hosts the websocket and allows clients to connect to it. I find this my go-to for quickly getting small projects up and running quickly - but if you're making something big (or plan on having lots of concurrent users) then a custom solution is probably better.
If you do really like PHP, there is an all-PHP solution too called Ratchet. This is great as you don't need to learn a new language if you don't know one already, and you know you're already going to be able to run it on your server without any additional software. Ratchet is built off ReactPHP, which is also an all PHP solution to get non-blocking IO that is event driven. The blogs for ReactPHP are helpful resources to get you started if you choose to go that route. You can check out links to get started with Ratchet or ReactPHP below:
Once you've got your backend done, JavaScript has a full WebSocket API you can use - you can find more information here:
How do I host WebSockets?
Hosting with WebSockets can be a bit of a pain, as it will require that you keep a service running constantly on the host server. However, there are many services that offer such a solution.
Typically, if it has SSH access, then you can make, configure and run services, although this is not always the case.
Amazon Web Services (AWS) and Google App Engine support this, and some other companies will as well - you'll just need to look around. You can also run your own server via those same services, which will let you run either of those libraries mentioned above.
One thing to bear in mind is that you do not need to run the WebSocket server on the same server that provides Web Hosting to you. So, you could use any sort of hosting for your site / domain that you want, and then have a separate service on AWS or App Engine providing your Socket server.