Starting to work on a new project using redis as a sub/pub system to display results from a mysql db. So if there are updates I wanted to publish those updates from mysql to my web page. My question is, which option would be better?
Option 1: Should I just do all of that via nodejs and socket.io? Meaning creating a nodejs script that connects to redis, subscribe to the channels I need to listen to, using mysql in nodejs query the db for updates, if updates publish the mysql rows then in the html that is connecting to nodejs via socket.io get the new data and process it to display the results?
Option 2: Have a php script query mysql and with redis-php client publish any updates to the channel? Dont know exactly what else needs to be setup from here. Do I still need to have nodejs involved in this option?
Or am I just off based on how all this works? The bottom line is I want to display results via mysql database to the user using redis sub/pub capabilities.
Option 3
When you update MySQL from PHP you publish those changes to node.js via redis
publish
command(publish from PHP when mutating database). From node.js I would receive those changes in real-time thanks to Redis's subscribe. Then I would just broadcast them to users interested via socket.io. You could for examplepublish
to channelmysql
. Take for example the following SQL statement =>INSERT INTO comments (1, "Hello World")
. Where1
is something like userid, andHello World
would be something like the comment. I probably would not publish SQL-statement to that channel, but JSON instead which I can easily use both from JavaScript(JSON.stringify / JSON.parse) and PHP(json_encode / json_decode).Update
You don't run a cron-job because this would defeat the purpose off Redis's pubsub. Take for example I visit your website which is a blog at
http://localhosts
. I read an article athttp://localhost.com/a.php
. Below on the site you provide a form which I can use to post a comment to that article:a.php
I submit the form which has action attribute
http://localhost/postcomment.php
. But this is the important part! Atpost.php
you retrieve the data I posted and insert it into MySQL usingINSERT INTO comments (1, "Hello World")
. When this mutation happens you also need to inform node.js process which is continually listening to channelmysql
:post.php:
post.php requires predis.
The node code with node_redis would look something like:
This samples depends on the following packages, which you can install via npm
Always when I post the form
post.php
, I also publish these changes to redis. This part is important! The node.js process is always receiving those changes thanks to Redis's pubsub. Every time when a php script mutates the database you should publish these changes to Redis withpublish
.P.S: Hope this is clear. Maybe later when I have some time available I update with maybe little snippet...