问题描述
开始使用Redis作为子/发布系统来处理新项目,以显示mysql数据库的结果.因此,如果有更新,我想将这些更新从mysql发布到我的网页.我的问题是,哪种选择会更好?
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?
选项1:我应该只通过nodejs和socket.io来完成所有这些工作吗?意思是创建一个连接到redis的nodejs脚本,订阅我需要收听的频道,在nodejs中使用mysql查询数据库以获取更新,如果更新发布了mysql行,则在通过socket.io连接到nodejs的html中获取新数据并对其进行处理以显示结果?
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?
选项2:是否有一个PHP脚本查询mysql,并使用redis-php客户端向该通道发布了任何更新?不知道从这里确切还需要设置什么.我是否还需要让nodejs参与此选项?
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?
或者我只是基于所有这些工作原理而离开吗?最重要的是,我想使用redis sub/pub功能通过mysql数据库向用户显示结果.
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.
推荐答案
选项3
从PHP更新MySQL时,您将通过redis publish
命令将这些更改发布到node.js(在更改数据库时从PHP发布).借助Redis的订阅,我可以从node.js实时接收到这些更改.然后,我将它们通过socket.io广播给感兴趣的用户.例如,您可以将publish
设置为通道mysql
.例如,下面的SQL语句=> INSERT INTO comments (1, "Hello World")
.其中1
类似于userid,而Hello World
则类似于注释.我可能不会将SQL语句发布到该通道,但是可以使用JSON代替,可以从JavaScript(JSON.stringify/JSON.parse)和PHP(json_encode/json_decode)轻松使用.
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, "Hello World")
. 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).
您不要执行cron-job,因为这会破坏Redis pubpub的目的.例如,我访问了您的网站,该网站是位于http://localhosts
的博客.我在http://localhost.com/a.php
上阅读了一篇文章.在网站的下方,您提供了一个表格,我可以使用该表格对该文章发表评论:
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>
<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
:
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, "Hello World")
. When this mutation happens you also need to inform node.js process which is continually listening to channel 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的节点代码如下所示:
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');
此示例取决于以下软件包,您可以通过npm安装
This samples depends on the following packages, which you can install via npm
npm install socket.io
npm install redis
npm install express
总是在发布表单post.php
时,还将这些更改发布到redis.这部分很重要!感谢Redis的pubsub,node.js进程始终收到这些更改.每次当php脚本使数据库发生突变时,都应使用publish
将这些更改发布到Redis.
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:希望这很清楚.也许以后有空的时候我会更新一些代码段...
这篇关于Redis子/发布和php/nodejs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!