Redis sub/pub and php/nodejs?

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 example publish to channel mysql Take for example the following SQL statement INSERT INTO comments (1, " World") Where 1 is something like userid, and 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 at http://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 html> Interesting blog post This is interesting Alfred Said at 22:34 World Your name Your Message: $_POST'name', 'message' => $_POST'message' ); $json = json_encode($obj); $redis->publish("mysql", $json); echo $json post. Php requires predis The node code with node_redis would look something like: var redis = require('redis'), subscriber = redis.createClient(), express = require('express'), store = new express.session.MemoryStore(), app = express.

CreateServer( express.bodyParser(), express. Static(__dirname + '/public'), express.cookieParser(), express. Session({ secret: 'htuayreve', store: store})) sio = require('socket.Io'); app.

Listen(8888, '127.0.0.1', function () { var addr = app.address(); console. Log('app listening on http://' + addr.address + ':' + addr.port); }); var io = sio. Listen(app); io.

Configure(function () { io. Set('log level', 1); // reduce logging }); io.sockets. On('connection', function (socket) { socket.

Join('mysql'); socket. On('disconnect', function () { }); }); subscriber. On('message', function (channel, json) { // this will always retrieve messages posted to mysql io.sockets.

In('mysql').json. Send(json); }); subscriber. Subscribe('mysql') This samples depends on the following packages, which you can install via npm npm install socket.

Io npm install redis npm install express 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 with publish P.

S: Hope this is clear. Maybe later when I have some time available I update with maybe little snippet.

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 example publish to channel mysql.

Take for example the following SQL statement => INSERT INTO comments (1, " Where 1 is something like userid, and 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 at http://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 Interesting blog post This is interesting Alfred Said at 22:34 World Your name Your Message: I submit the form which has action attribute http://localhost/postcomment.php. But this is the important part!

At post. Php you retrieve the data I posted and insert it into MySQL using INSERT INTO comments (1, " When this mutation happens you also need to inform node. Js process which is continually listening to channel mysql: post.

Php: $_POST'name', 'message' => $_POST'message' ); $json = json_encode($obj); $redis->publish("mysql", $json); echo $json; post. Php requires predis. The node code with node_redis would look something like: var redis = require('redis'), subscriber = redis.createClient(), express = require('express'), store = new express.session.MemoryStore(), app = express.

CreateServer( express.bodyParser(), express. Static(__dirname + '/public'), express.cookieParser(), express. Session({ secret: 'htuayreve', store: store})) sio = require('socket.

Io'); app. Listen(8888, '127.0.0.1', function () { var addr = app.address(); console. Log('app listening on http://' + addr.address + ':' + addr.port); }); var io = sio.

Listen(app); io. Configure(function () { io. Set('log level', 1); // reduce logging }); io.sockets.

On('connection', function (socket) { socket. Join('mysql'); socket. On('disconnect', function () { }); }); subscriber.

On('message', function (channel, json) { // this will always retrieve messages posted to mysql io.sockets. In('mysql').json. Send(json); }); subscriber.

Subscribe('mysql'); This samples depends on the following packages, which you can install via npm npm install socket. Io npm install redis npm install express 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 with publish.P. S: Hope this is clear. Maybe later when I have some time available I update with maybe little snippet...

– John Jul 31 at 22:57 updated answer and also include a little example! Hopefully it is clear now you don't need cronjobs but just use Redis's pubsub. – Alfred Aug 1 at 2:30 Ok I think I understand.

I believe the problem I was having wrapping my head around this was the whole client side update process. Kept thinking I had to query the db every so many seconds to get updates when it should only get updated when an action happen on the client side. IE adding comments, deleting comments, editing comments.

Just so used to the client side doing all the work. – John Aug 1 at 13:57 node. Js is a process which is always running and available in the whole context(each request knows about the other request), while each PHP process is separated(each request does not know anything about other request).

That's what you should wrap your head around. – Alfred Aug 1 at 14:09 1 Redis is a very fast advanced key-value store which also has pubsub. Redis uses plain sockets.

This means it does not use HTTP. I like to use Redis's pubsub to communicate with PHP in a very fast manner over sockets. To send changes in real-time to all browser we need socket.io.

I hope this makes it all clear. – Alfred Jul 311 at 0:03.

When you update MySQL from PHP you publish those changes to node. Js via redis publish command(publish from PHP when mutating database). 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 example publish to channel mysql. Take for example the following SQL statement => INSERT INTO comments (1, " Where 1 is something like userid, and Hello 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).

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 at http://localhost.com/a.php.

I submit the form which has action attribute http://localhost/postcomment.php. But this is the important part! Php you retrieve the data I posted and insert it into MySQL using INSERT INTO comments (1, " When this mutation happens you also need to inform node.

Php requires predis. Always when I post the form post. Php, I also publish these changes to redis.

This part is important! 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 with publish.

S: Hope this is clear. Maybe later when I have some time available I update with maybe little snippet...

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions