Redis Pub-Sub or Socket.IO's broadcast

前端 未结 2 459
北恋
北恋 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:41

    The reason I chose to use Redis Pub Sub with Socket.io in my Real Time Activity Stream project (http://blog.cloudfoundry.com/2012/06/05/node-activity-streams-app-2/) was because I wanted to have multiple web servers (instances on Cloud Foundry or dynos on Heroku). As far as I could see, Socket.io stores the messages in memory (of one webserver), so how it could possibly broadcast to clients which are connected to another webserver?

    Check out the post and let me know if it helps

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题