Explanation about pub sub (redis)

2

I searched pub sub and did not find anything in Portuguese, so I decided to ask here, I wanted to understand how pub and sub works especially for to be applied with redis , I read something about it, but I did not quite understand how it works.

Would it be to create channels on which users could subscribe, and everyone on that channel could read the message? for example a game where the position of the objects and of the players is shared, these channels could be areas of the map that would be created from appearing players online. Where would the data be?

    
asked by anonymous 20.10.2017 / 23:16

1 answer

2

The data remains in the server's memory, eventually stored on disk according to the redis settings. When a client receives the data, it stays in the client's memory until you do something with that data.

PubSub is a real-time notification engine. I think you already have a good idea about it.

In a notification system, for example a chat, John and Mary are in a direct conversation. Alex's customer does not need to get notifications from their conversation, so PubSub will help resolve this issue. With it, you can create notification publishing channels ( Pub ), and then you enter the clients that should receive them ( Subs ), just to segregate by any notifications for customers. In chat, you could create a "JoãoxMaria" PubSub channel in which only the customer of both is subscribed. There is a public chat in which everyone can talk to each other, so these notifications everyone should receive, in which case everyone is subscribed ( sub scripted) in that pub-serving channel) .

The difference between PubSub and other methods of transmission is that you are not specifying each subscriber when sending a message, you are simply specifying the channel, not knowing which subscriber you are subscribing to. The advantages are greater scalability and less coupling (publishers do not need to know about subscribers).

You can play around in the terminal to test this, see:

    
21.10.2017 / 05:35