Redis Pub-Sub or Socket.IO's broadcast

前端 未结 2 481
北恋
北恋 2020-12-25 15:34

I saw this snippet:

On Server

io.sockets.on(\'connection\', function(socket) {
  const subscribe = redis.createClient();
  const publish = redis.cr         


        
2条回答
  •  情书的邮戳
    2020-12-25 15:46

    Redis is used here for at least two reasons. The first one is that it works really well as a publish and subscribe mechanism, including being able to select messages based on pattern matching to psubscribe. The second reason is the ease of other clients to be able to publish and subscribe through the redis mechanism. That is not to say that it is impossible, just convenient. This combined with the asynchronisity of node.js makes a powerful partnership.

    It's not the only solution of course but one that seems to work rather well.

提交回复
热议问题