Redis pub/sub for chat server in node.js

前端 未结 2 1380
死守一世寂寞
死守一世寂寞 2021-01-31 12:41

I\'m trying to work the Redis Cookbook example:

var http = require(\'http\'),
io = require(\'socket.io\')
fs = require(\'fs\'),
redis = require(\'redis\'),
rc =          


        
相关标签:
2条回答
  • 2021-01-31 13:01

    I believe that the example from that book is missing something, I also read that book and wondered. You are subscribed to the Redis channel and are waiting for messages on the server side, but you never publish to that channel. What is missing is an event listener so when there is a websocket message, you publish that message to the redis channel.

    0 讨论(0)
  • 2021-01-31 13:08

    If you are using redis pub/sub functionality within your node.js program you should dedicate one redis client connection for listening on some channel and second redis client connection for sending normal commands and/or publishing messages to your channel(s). From node_redis docs:

    When a client issues a SUBSCRIBE or PSUBSCRIBE, that connection is put into "pub/sub" mode. At that point, only commands that modify the subscription set are valid. When the subscription set is empty, the connection is put back into regular mode.

    If you need to send regular commands to Redis while in pub/sub mode, just open another connection.

    Your problem is also related to these questions:

    • Redis / Node.js - 2 clients (1 pub/sub) causing issues with writes
    • Why can't I have a single Redis client acting as PUB and Sub in the same connection?
    0 讨论(0)
提交回复
热议问题