Redis does simple messaging. In this video, learn what that means and how to do it.
- [Instructor] Welcome to Redis Messaging, part of the course Build Complex Express Sites with Redis and Socket.io. In this section, we will cover Redis' ability to do messaging. Now we will do that by looking first at channels, subscribe, then publish, and finally we'll put it all together. This is the first message of the section, the video's named Channels and Subscribe. We'll get a good idea of what messaging is and how to create channels.
We will do this by covering what Redis messaging is, and then we'll talk about what channels and subscribing is in the context of Redis messaging. So let's go ahead and get started. So first of all, the main focus of Redis is the in-memory data structure store. That is what we covered in the last section. But, Redis also has another feature, and that is messaging. Messaging is a way for processes to send messages between themselves. If we have a program and it needs to do something, we just execute the function, we pass in the data that's needed for that function or method.
Now what if the programs scale beyond just one process? How would we send information in between that? How would we execute another function or method in another process? That is where messaging comes in. We can send a message from one process to another process. I do want to note that messaging can be way more complex than just what we are simply explaining here, but this does get across what messaging is. Now Redis implements a very simple form of messaging called PUB/SUB.
PUB/SUB stands for Publish Subscribe. It really just breaks down to one process, adding a message, and another process that can receive that message. So how do we do this? Well, this is where we get right into channels and subscribe. So starting with channels. Channels are the methods in which we can listen for messages or add messages. Because Redis has a simple system, there really is not more to it than that. Now, when naming and using channels, we can duplicate some of the logic we use when creating keys.
For example, we can namespace a channel by prepending it with what it's related to. For example, if we needed to let another process or program know that a user has been created, we would create a channel named user:create, and then in the message pass in the user ID. Now there are no channel commands, so we don't really have anything to run here, but we can run commands for subscribe. So let's look at that. So we'll jump to the terminal, and again make sure we have the Redis server running, and so subscribe is really where we have one of the processes, say I want to listen for these messages.
So, let's go ahead and use that command. Now again, all the docker commands are in a file, docker command dot text, so I'm gonna run one to connect to use Redis command line interface. And then to use it, we just do subscribe as you can see, then it says what channel you want to subscribe to. Then we can subscribe to multiple channels, but here we're just gonna put one in, and like we said before, we'll use user:create. And then this is sitting right here, ready for a message to come in.
Now, we'll talk about sending messages in the next video. But for now, this is really all you need to do. Again, Redis has very simple messaging, so the commands are actually really simple. Now there's actually another command that we can do, and I'll go ahead and do that. Put in here connect to Redis, and it's psubscribe. The P stands for a pattern. So this allows us to do simple matching. You know, we can do a question mark to match a letter or a star to match multiple letters.
So if we wanted to do something that matched all user events we could do psubscribe, and we put in the pattern. So here we would say user:*. And this'll match anything that starts with user:. So if we namespaced all our events, all our messages to user:, this would listen to all of them. Okay, so that finishes up what we're gonna talk about with channels, which it is just a string, again pretty much anything with Redis is very simple, which is actually really nice.
And then we talked about subscribe and psubscribe, which is a way that you can actually connect to these channels and listen for messages. So in the next video, we will cover the message creation command Publish. This will allow us to start sending messages back and forth.
Note: This course was created by Packt Publishing. We are pleased to host this training in our library.
- Using Redis
- Structures, channels, and subscribing
- Adding messaging
- Integrating Redis and Node.js
- Working with geospacial indexes
- Using Node.js with Socket.IO
- Working with rooms
- Creating namespaces
- Using Socket.IO and Express
- Sharing states
- Using routes