如何使用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.
拉链里面有什么
./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>
启动服务器
Start server
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).
但是在发布
到频道 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屋!