I'd like to implement a dynamic vote system and need to combine PHP and Java with a MariaDB 10.3.
What I want to do:
When a user votes, the vote is added to the database, a random is generated and the user gets an email to verify the vote by click a link. When the vote is verified, the database should somehow send an event to a Java client so it gets notified about the data change so then the client on my system can update the overall votes. If possible, I'd like to not to poll the server frequent but idle and get an event. Maybe somehow know other ways to send an event from the PHP script to Java?
This might help: This is my root server - so if needed, I can do any modification to enable PHP to somehow talk to a socket or run a local binary - but for security reasons I'd like not to, or at least don't want to open security holes just to get this working.
Thanks in advance,
another possibility (if someone is willing to help) I could also setup an application server - but would need some help to get it working along already running apache httpd
If it's a Java web application, the PHP server can simply post a web services API call to the Java webapp.
If it's an OSGi application, a web service might also work.
If it's a stand-alone application, you have several options. A popular one would be to use MQ services, which is how complex multi-component systems like the OpenStack cloud keep their components informed.
And there are many other possibilities.
When it comes to destroying a civilization, gas chambers cannot hold a candle to echo chambers.
I'm sorry for not specifying it clear enough. Let me try:
Currently on my root runs an opensuse 15.1 with Apache2 httpd and PHP7 (both in distribution specific current versions - if needed I have to look them up). Also I setup an Apache James mail server v3.3.0 and some current sendmail as so called nullclient (that's it just takes messages dropped in local via sendmail command and forwards it to the james server wich handles all the MTA stuff). So Apache httpd is able to send mails via php mail() function.
Now I'd like to set up a voting system (reason doesn't matter here) wich works like this:
A user can choose from different vote options and has to provide a name and a valid e-mail-address (yes, I'm also setting up security to prevent this script gets misused as spam interface). When the user submit the vote, it is saved into the database but marked as non validated. To do so, the save script also generates a random token and sends it with the id of the newly created entry via mail the user has to interact with to verify. When this verify happen all that will done is that a flag in the dataset is flipped so it's marked as validated.
On the same time a stand-alone Java code runs on the same system also connected to the same database. In addition to that a code running on my PC is connected to this server code via simple TCP cinnection.
What should happen, when a user verifies a vote: an event should be triggered so that the java code run on server can send the just verified vote to the client on my PC wich then updates the total vote along with a possible message added to the vote.
Where I'm stuck: although it would be simple to add another flag to the dataset and poll those not yet polled, it's still polling. As I expect only low user count most of time/poll would be wasted. That's why I want it event based so that somehow the php script for verification also informs the java code without the java code has to poll the database all the time.
A way I could think of: use php socket() function to open a socket to the java code - although it's local I'd prefore to not to enable socket/files for php.
Another way I could think of would be a java application server: as I somehow then already be in java-land rest should be cake - although a fullblown app server seems overkill for a simple event.
Matt Wong wrote:As I expect only low user count most of time/poll would be wasted.
Wasted? Is the server which would be doing the polling fully committed to doing other things? I suspect it isn't. In which case there's nothing wrong with taking some time in which the server would have been doing nothing, and have it poll the database and find there's nothing to do.
Short: I see active polling as bad concept when an event driven approach is possible.
Let's say we cut out Java and try it only with PHP -> my browser still has to poll the server every now and then, there simply is no technique available in stateless HTTP/1.1 so the server could trigger an event actively back in my browser without my browser polling it (also long-polling still requires active request from browser - only the response is just held back - wich could lead to timeout issues). Even if I set the poll timeout to 5 or 10 secounds - it's still simple waste of resources for power-intensive crypto just to get a "no update" instead of NIC-level TCP keep alive packets costing way less.
No way I try it - there is always some polling involved - unless I open potential security holes by allowing risky operations like fsockopen(). So, even may some think my way seems "even more stupid" - I think using mail() is a possible "active event" way.
How it works: The script checking the AUTH link calls mail() wich sends a mail local from apache to james - and my client simple keep IMAP IDLE and will get informed active by james wich generates an event I can process. IMAP IDLE by RFC is handled by TCP keep-alive as long as the client not issue other commands interrupting the IDLE - although it depends on server and client specific implementation to conform with RFC to not interrupt IDLE by fetching mails affected by change (new mails, flag changes, delete, etc).
This way I have an all-active forward only way of events:
user votes > user get mail to verify vote > user actively verifies > server sends mail local > mail-server trigger event on client wait in IDLE > my local client can process further actions - so I get rid of any polling (by RFC IMAP IDLE is not considered polling - as polling would require process of event and re-enter polling - wich imap IDLE does not when implemented correctly).