How to use redis PUBLISH/SUBSCRIBE with nodejs to

2019-01-03 19:22发布

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.

8条回答
聊天终结者
2楼-- · 2019-01-03 19:38

Complete Redis Pub/Sub Example (Real-time Chat using Hapi.js & Socket.io)

We were trying to understand Redis Publish/Subscribe ("Pub/Sub") and all the existing examples were either outdated, too simple or had no tests. So we wrote a Complete Real-time Chat using Hapi.js + Socket.io + Redis Pub/Sub Example with End-to-End Tests!

https://github.com/dwyl/hapi-socketio-redis-chat-example

The Pub/Sub component is only a few lines of node.js code: https://github.com/dwyl/hapi-socketio-redis-chat-example/blob/master/lib/chat.js#L33-L40

Rather than pasting it here (without any context) we encourage you to checkout/try the example.

We built it using Hapi.js but the chat.js file is de-coupled from Hapi and can easily be used with a basic node.js http server or express (etc.)

查看更多
我只想做你的唯一
3楼-- · 2019-01-03 19:41

Update to the code:

staticProvider

now renamed to

static

see migration guide

查看更多
家丑人穷心不美
4楼-- · 2019-01-03 19:43

here's a simplified example without as many dependencies. You do still need to npm install hiredis redis

The node JavaScript:

var redis = require("redis"),
    client = redis.createClient();

client.subscribe("pubsub");
client.on("message", function(channel, message){
  console.log(channel + ": " + message);
});

...put that in a pubsub.js file and run node pubsub.js

in redis-cli:

redis> publish pubsub "Hello Wonky!"
(integer) 1

which should display: pubsub: Hello Wonky! in the terminal running node! Congrats!

Additional 4/23/2013: I also want to make note that when a client subscribes to a pub/sub channel it goes into subscriber mode and is limited to subscriber commands. You'll just need to create additional instances of redis clients. client1 = redis.createClient(), client2 = redis.createClient() so one can be in subscriber mode and the other can issue regular DB commands.

查看更多
smile是对你的礼貌
5楼-- · 2019-01-03 19:43

Handle redis errors to stop nodejs from exiting. You can do this by writing;

subcribe.on("error", function(){
  //Deal with error
})

I think you get the exception because you are using the same client which is subscribed to publish messages. Create a separate client for publishing messages and that could solve your problem.

查看更多
beautiful°
6楼-- · 2019-01-03 19:48

Check out acani-node on GitHub, especially the file acani-node-server.js. If these links are broken, look for acani-chat-server among acani's GitHub public repositories.

查看更多
贼婆χ
7楼-- · 2019-01-03 19:50

If you want to get this working with socket.io 0.7 AND an external webserver you need to change (besides the staticProvider -> static issue):

a) provide the domain name instead of localhost (i.e. var socket = io.connect('http://my.domain.com:3000'); ) in the index.html

b) change HOST in app.js (i.e. const HOST = 'my.domain.com'; )

c) and add sockets in line 37 of app.js (i.e. 'socket.sockets.on('connection', function(client) { …' )

查看更多
登录 后发表回答