I have written a program in PHP that continually updates a database. I would like it to spit out data as it runs, so that other processes can read it and do stuff with it.
Here's what I mean in a diagram:

I realize that I'm looking for some sort of inter-process communication (IPC) to achieve this, but I'm not sure what the easiest/simplest way to do it is in PHP.
Here are two possible solutions I've come up with:
1. Turn the Main Process in to a Unix Socket server.
I could add some socket code to the Main Process to turn it in to a Unix Socket server, handling connections and writing to the socket as it runs. Other processes can then connect and read data from the socket:

However, this means that the Main Program will have to deal with being both a socket server along with doing its primary job. Furthermore, PHP doesn't support threading out of the box, which makes handling multiple connections a little trickier, and so the whole program will become much more complex.
In all, this doesn't seem like the simplest solution, especially for a program written in PHP (unfortunately).
2. Create a separate Unix Socket server.
Alternatively, I could write a separate Unix Socket server program that the Main Process can write to, and it will relay all writes to any other process that connects to it:

This has the benefit of separating the socket serving responsibilities from the Main Process. It would also make the connection handling much simpler as I could write it in a language that is better suited to being a socket server.
But on the other hand, it does mean that I will need to write and run an additional program to help me handle the inter-process communication.
Is there a simpler solution?
Option 2 is my preferred solution so far, but is there some alternative method that I may be missing here?
Again, I just want an existing PHP process to spit some data out as it runs, and for other processes to be able to read from it.
What are some simple solutions for this kind of interprocess communication?
You are looking for something like ActiveMQ, this will allow you to "publish" your data to a queue(s), then you can have other scripts/processes as subscribers to this queue. Essentially the subscribers would pull data as it becomes available. It's very configurable and easy to use for purposes of distributing data.
You may also use classes such as PHP Stomp to make implementation of messaging easier.
Apache ActiveMQ
PHP Stomp
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With