Explanation about sub pub(redis)

Asked

Viewed 2,325 times

6

I researched about sub pub and I didn’t find anything in Portuguese, so I decided to ask here, I wanted to understand how the pub and sub especially to be applied with the redis, I read something about, but I couldn’t quite understand how it works.

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

1 answer

5


The data stays in the server memory, eventually stored on disk according to the redis settings. When a customer receives the data, it stays in the customer’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, João and Maria are in a direct conversation. Alex’s client doesn’t need to receive notifications from their conversation, so Pubsub will help resolve this issue. With it, you can create notification posting channels (Pub), and then you enroll in them the customers who should receive them (Subs), only to segregate by any criteria the notifications to customers. In chat, you could create a "Joãoxmaria" Pubsub channel in which only the client of both is subscribed. There is a public chat where everyone can chat with each other, so these notifications everyone should receive, in which case everyone is subscribed (Subscripted) in this publication channel (Pub).

The difference between Pubsub and other transmission methods is that you are not specifying each subscriber when sending a message, you are simply specifying the channel, not knowing which subscribers are (not even knowing if there are subscribers). The advantages are higher scalability and lower coupling (publishers don’t need to know about subscribers).

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

asciicast

  • So that’s just what I was thinking, it’s going to be a good one for me, I’m building the multiplayer for a game, and I need to share information among the players, but it would be impossible to share everything with everyone, but I can create these channels of small areas around the players, and anyone entering and leaving this area would be enrolled or removed from the channel, my question is: create and delete channels is simple how to subscribe to them? because in time it would be necessary to "clean up" so that there was no one else in the channel area for a long time.

  • And how would I do this control? Would players send their position to the channel with a PUBLISH? That I didn’t quite understand, or for storage, whether cached or disk, and the channels would pick up from there? I can’t visualize that. Try it here: Maria is a player, she’s online, so an area around her will be created as a channel, where she would specify what information would go to Maria, just one example, all enemies who are in that area? John entered the area of the channel, now also receives this information and also the post of Mary...

  • I create the channel when I type "SUBSCRIBE all". If it doesn’t exist, create it and sign up. If it already exists, just sign up. I did nothing before starting to record the asciinema, that is straight after starting the redis server. I have no idea if this would be appropriate in an online multiplayer. I think it depends a lot on the game, but I have no experience with it.

  • but there you send the message directly from the channel, and if John wants to send a message? type goes to the channel and then to all of the channel? how is this?

  • @Perdugames I’m sending all the messages directly from another redis-cli, just to show PUBLISH, without worrying about "who" is sending. Clockwise, the first three panels are customers of the redis subscribed in channels and on the last panel I am sending messages. To identify the author, you would need to form a message like: {autor_id: 1, message: "olá"}. The fold is completely alien to these things, it only delivers and receives messages.

  • There is no user system in redis, you would have to deploy as message metadata, understand? at all 4 terminals, the environment is the same: a redis-cli

  • then I think I should send the information of customers who are on a channel somewhere and transmit it to the whole channel. That would be it?

  • @Perdugames What would you be trying to accomplish with this?

  • I am not able to understand, how would be done the sending of messages, John sends a message and it goes where? as she goes to the channel to go to all of the channel?

  • do not understand if your question has to do with the transport mechanism or distribution of redis, or just the use

  • a quick question that came to me, if I want to be able to have multiple users and channels, I need then for each user to create a client in redis to subscribe to the channel, right? and how would I receive the messages? I tried so, but I don’t think it is because it didn’t work. clients[userID]['cliRedis'].on("message", function(channel, message) {...}); would have to make a loop for customers?

  • This has to do with some implementation, some specific language and specific library. Open a new question.

  • https://answall.com/questions/248871/escutar-mensagens-com-v%C3%A1rios-clientepub-sub-redis feita

Show 9 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.