Option 3
当您从 PHP 更新 MySQL 时,您可以通过 redis 将这些更改发布到 node.jspublish
命令(在更改数据库时从 PHP 发布)。由于 Redis 的订阅,我可以从 Node.js 实时收到这些更改。然后我会通过 socket.io 将它们广播给感兴趣的用户。例如你可以publish
引导mysql
。以下面的 SQL 语句为例 =>INSERT INTO comments (1, "Hello World")
. Where 1
类似于 userid,并且Hello World
会像评论一样。我可能不会将 SQL 语句发布到该通道,而是发布 JSON,我可以轻松地从 JavaScript(JSON.stringify / JSON.parse) 和 PHP(json_encode / json_decode) 中使用它。
Update
您不运行 cron 作业,因为这会违背 Redis pubsub 的目的。以我访问您的网站为例,该网站是一个博客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
你检索我发布的数据并将其插入 MySQL 使用INSERT INTO comments (1, "Hello World")
。当这种突变发生时,你还需要通知不断监听通道的node.js进程mysql
:
帖子.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 https://github.com/nrk/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 脚本改变数据库时,您应该将这些更改发布到 Redispublish
.
PS:希望这是清楚的。也许稍后当我有时间的时候我会更新一些小片段......