How do you use what you know about Redis with Node.js?
- [Instructor] Welcome to the section, Integrate Redis and Node JS. This is part of the course Build Complex Sites with Redis and Socket IO. In this section, we will cover some ways that we can use Redis and Node JS in Express, so this should touch on all the things we covered in the previous sections about Redis, but then in the context of using it in Node JS. As we can see, we're going to start off by just integrating it and then touch on everything that we talked about with using Redis and doing that in Node JS.
In this video, we will specifically be dealing with integrating Redis. This is the first step from going from the Redis command line interface to actually using Node JS with Redis. So we will start off by introducing a client we will use and then we'll actually use Node JS with that client and Redis. So this is where we apply the things we learned in the previous sections. We should have a good foundation of what Redis is and how it works. We will now see what this means when using Node JS.
First, we need to cover is how to connect to Redis when in Node JS. So we'll need a client to do that. So let's actually open a browser and go to the Redis website. So, as you can see on the Redis website, they have a nice list of recommended clients when using specific languages or frameworks. Now, if we notice here, they have Node JS, so let's go there and we see it lists quite a few here that you can use. Now we specifically will be using Node Redis which is a recommended client for Node by Redis.
So if we click this link, we'll actually go to the GitHub of the project and from here, we can see, this tells us how to use it and all the commands that we can do with this library. Now, what is great about this library is that we already know how to use it. This is because every command is actually a method off of the client that when you connect. So for example, we can see it here, Client set, that runs the set command, and this is true for every other command that we can run in Redis.
So we've used a Redis command line interface, we know how to use this client library. Now when using it, it returns with the classis Node JS form meaning that we will have to add a callback that has an error as the first parameter and then the data as a second parameter and we can deal with it there. So let's go ahead and take a quick look at how to do this. So let's open up the directory, section three one, and take a look at the package.json that we have there. So let's do that now.
So as you can see, in NPM, the package name is actually Redis. And we're just installing the current version which is 2.6.2. Now, in addition to this, we're using Express, so, now let's actually look at how to use it. So we'll do that by jumping into the index file and then we're going to require Express and we're going to require Redis and then we have this config.js, so let's take a quick look at that. So looking at that, all we're doing is we're setting the connection information.
Now, because we're running this in Docker, it's going to be the local host, and then we are setting it to this port, so this is how we're actually gonna connect to the Redis docker container from Node JS. So let's go back to index.js. So here we come in, we create a client by using the port and the host. And again, if you're using this locally, make sure that your docker container is running and of course, there's a dockercommand.text that runs through all the commands that you may need to use to have that docker container running.
So here, we're gonna set this key called redis key and we're gonna set it to zero and then we're gonna run the increment function which remember, takes whatever's at that key and then just increments it by one and then we're gonna get that key, and again, so here is our classic Node JS function, where we have the error and the reply, and we're gonna take the reply of that, and we're gonna stick it in this very basic HTML so it'll say Our Redis and Express Web Application and give us a count that is in Redis key.
And then we will send this and we're gonna listen on 8080, so if we go to a console and then launch this, with Node index JS and then run this in a browser. So as you can see, the first request, we get a count of one, which makes sense, because we set it to zero and after the first request, we incremented it once. Now, each time we refresh it, is gonna count up because it's storing it in Redis.
So this demonstrates the fact that we can store information between requests. So for example, this would be a great way to store session data. So in between each request, we could store something about that user or client. But with Redis, we can do even more. So we can actually store information between servers. So let's jump back to the code really quick. So we're going to make a slight modification to the code. We're going to comment out the resetting of zero because every time we launch this, this would set that back to zero and instead of listening on 8080, we're going to say listen for the argument that's passed in to our command.
So I will save this and then now what we're gonna do, is we're gonna launch two processes, so I'm gonna run one on 8080 by running Node index js 8080 and then run another by saying node index js 8081 and then we'll load up both of those in tabs. So here we are back in the browser, so I'm connecting to 8080 and 8081.
So I have two processes running this. Now again, these are on the same computer, but technically, these could be on completely different servers in completely different parts of the world but if they both can connect to the same Redis, they actually can share information between these pages. So let's reload this, so we see this is counting up nine, 10, 11, 12. If we come back here, I launched this, so it got eight, and if they're sharing information, if this is 12 when we run this, this should increment, this should be 13 because they're storing the same information and going from there.
So as you can see, we are actually sharing that counter between two separate processes. So in this video, we were able to start using Redis and Node JS. So we used this by grabbing the NPM library, Redis. Now this is a very easy and simple to use library where each command that we learned previously is actually a method off of the client. In addition to that, this also uses the common Node JS practice of using a callback that has an error and a reply with the data. So then we took that library and we put it in a simple site that showed how we can share data between requests, and even further, how we can share data in between two different servers.
Now in the next video, we'll see how we can actually share messages between Node JS instances.
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