8

Hello I was wondering if there is something that allows us to use real-time updates using php with it.

I Learnt node.js with the hope to solve the thing but I didn't know there are very few hosting options to host a site that uses php and node.js and I don't like them.

Here is my tech stack:

  1. PHP
  2. MySql
  3. CSS
  4. HTML
  5. javascript (Ajax obviously)

What is the best option for real-time updates without having to use ajax?

1 Answers1

25

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.

FontFamily
  • 355
  • 4
  • 13
Toastrackenigma
  • 7,604
  • 4
  • 45
  • 55
  • 1
    Excellent Just the answer I was looking for, it is very helpful for me. Thank you a lot Toastrackenigma! –  Oct 07 '17 at 02:44
  • While this question was closed for not meeting SO's requirements, the answer is 100% helpful. This question showed up first for my Google query. Thanks for putting this comprehensive answer together. Turns out Ratchet is built off ReactPHP. ReactPHP has nothing to do with the JS React library. I've slightly edited your answer to help clarify some of these things. – FontFamily Sep 16 '21 at 14:43
  • Best Answer . – Jack Tech Feb 07 '22 at 16:36