当数据值发生变化时,如何将 redis PUBLISH/SUBSCRIBE 与 nodejs 结合使用来通知客户端? [英] How to use redis PUBLISH/SUBSCRIBE with nodejs to notify clients when data values change?
问题描述
我正在使用 NodeJS 和 Redis 编写一个事件驱动的发布/订阅应用程序.我需要一个示例,说明如何在 Redis 中的数据值更改时通知 Web 客户端.
I'm writing an event-driven publish/subscribe application with NodeJS and Redis. I need an example of how to notify web clients when the data values in Redis change.
推荐答案
OLD 只使用参考
依赖项
使用 express、socket.io、node_redis 最后但并非最不重要的示例代码 来自媒体大火.
OLD only use a reference
Dependencies
uses express, socket.io, node_redis and last but not least the sample code from media fire.
首先你应该(如果你还没有这样做)在 30 秒内安装node.js+npm(正确的方法,因为您应该不以 root 身份运行 npm):
First you should(if you have not done this yet) install node.js+npm in 30 seconds (the right way because you should NOT run npm as root):
echo 'export PATH=$HOME/local/bin:$PATH' >> ~/.bashrc
. ~/.bashrc
mkdir ~/local
mkdir ~/node-latest-install
cd ~/node-latest-install
curl http://nodejs.org/dist/node-latest.tar.gz | tar xz --strip-components=1
./configure --prefix=~/local
make install # ok, fine, this step probably takes more than 30 seconds...
curl http://npmjs.org/install.sh | sh
安装依赖
安装 node+npm 后,您应该通过发出以下命令来安装依赖项:
Install dependencies
After you installed node+npm you should install dependencies by issuing:
npm install express
npm install socket.io
npm install hiredis redis # hiredis to use c binding for redis => FAST :)
下载样本
您可以从 mediafire 下载完整示例.
unzip pbsb.zip # can also do via graphical interface if you prefer.
zip 里面有什么
./app.js
const PORT = 3000;
const HOST = 'localhost';
var express = require('express');
var app = module.exports = express.createServer();
app.use(express.staticProvider(__dirname + '/public'));
const redis = require('redis');
const client = redis.createClient();
const io = require('socket.io');
if (!module.parent) {
app.listen(PORT, HOST);
console.log("Express server listening on port %d", app.address().port)
const socket = io.listen(app);
socket.on('connection', function(client) {
const subscribe = redis.createClient();
subscribe.subscribe('pubsub'); // listen to messages from channel pubsub
subscribe.on("message", function(channel, message) {
client.send(message);
});
client.on('message', function(msg) {
});
client.on('disconnect', function() {
subscribe.quit();
});
});
}
./public/index.html
<html>
<head>
<title>PubSub</title>
<script src="/socket.io/socket.io.js"></script>
<script src="/javascripts/jquery-1.4.3.min.js"></script>
</head>
<body>
<div id="content"></div>
<script>
$(document).ready(function() {
var socket = new io.Socket('localhost', {port: 3000, rememberTransport: false/*, transports: ['xhr-polling']*/});
var content = $('#content');
socket.on('connect', function() {
});
socket.on('message', function(message){
content.prepend(message + '<br />');
}) ;
socket.on('disconnect', function() {
console.log('disconnected');
content.html("<b>Disconnected!</b>");
});
socket.connect();
});
</script>
</body>
</html>
启动服务器
cd pbsb
node app.js
启动浏览器
如果你启动谷歌浏览器最好(因为 websockets 支持,但不是必需的).访问 http://localhost:3000
以查看示例(一开始你什么都看不到,只有 PubSub
作为标题).
Start browser
Best if you start google chrome(because of websockets support, but not necessary). Visit http://localhost:3000
to see sample(in the beginning you don't see anything but PubSub
as title).
但是在 publish
到频道 pubsub
上,您应该会看到一条消息.下面我们发布"Hello world!"
到浏览器.
But on publish
to channel pubsub
you should see a message. Below we publish "Hello world!"
to the browser.
publish pubsub "Hello world!"
这篇关于当数据值发生变化时,如何将 redis PUBLISH/SUBSCRIBE 与 nodejs 结合使用来通知客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!