选项 3
当您从 PHP 更新 MySQL 时,您通过 redis publish 命令将这些更改发布到 node.js(在变异数据库时从 PHP 发布)。由于 Redis 的订阅,我可以从 node.js 实时收到这些更改。然后我会通过 socket.io 将它们广播给感兴趣的用户。例如,您可以将publish 引导至mysql。以下面的 SQL 语句 => INSERT INTO comments (1, "Hello World") 为例。其中1 类似于用户ID,Hello World 类似于评论。我可能不会将 SQL 语句发布到该通道,而是使用 JSON 代替,我可以轻松地从 JavaScript(JSON.stringify / JSON.parse) 和 PHP(json_encode / json_decode) 使用它。
更新
您不会运行 cron-job,因为这会破坏 Redis 发布订阅的目的。以我访问您的网站为例,该网站是http://localhosts 的博客。我在http://localhost.com/a.php 阅读了一篇文章。在网站下方,您提供了一个表单,我可以使用该表单对那篇文章发表评论:
a.php
<html>
<head>
<title>Interesting blog post</title>
</head>
<body>
<div id="article">This is interesting</div>
<div id="comments">
<div class="comment">
<div class="from">Alfred Said at 22:34</div>
<div class="message">Hello World</div>
</div>
</div>
<form action="post.php" method="post">
<label for="name">Your name</label><br />
<input type="name" id="name" name="name" /><br />
<label for="message">Your Message:</label><br />
<textarea id="message" name="message"></textarea>
<input type="submit" />
</form>
<script src='jquery.min.js'></script>
<script src='http://localhost:8888/socket.io/socket.io.js'></script>
<script type="text/javascript">
$(document).ready(function () {
var socket = io.connect('http://localhost:8888');
socket.on('message', function (json) {
var obj = $.parseJSON(json);
alert('in here: ' + obj.name);
});
});
</script>
</body>
</html>
我提交了具有动作属性http://localhost/postcomment.php 的表单。但这是重要的部分!在post.php,您检索我发布的数据并使用INSERT INTO comments (1, "Hello World") 将其插入MySQL。当这种突变发生时,您还需要通知 node.js 进程,该进程不断地监听频道mysql:
post.php:
<?php
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
require("./Predis.php");
$redis = new Predis\Client();
$obj = array(
'name' => $_POST['name'],
'message' => $_POST['message']
);
$json = json_encode($obj);
$redis->publish("mysql", $json);
echo $json;
post.php 需要predis。
带有 node_redis 的节点代码如下所示:
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');
此示例依赖于以下包,您可以通过 npm 安装这些包
npm install socket.io
npm install redis
npm install express
当我发布post.php 表单时,我也会将这些更改发布到redis。这部分很重要!多亏了 Redis 的 pubsub,node.js 进程总是会收到这些更改。每次当 php 脚本改变数据库时,您应该使用 publish 将这些更改发布到 Redis。
PS:希望这很清楚。也许稍后当我有空时,我可能会更新一些小sn-p...