{"id":1262,"date":"2018-10-23T00:11:06","date_gmt":"2018-10-23T00:11:06","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=1262"},"modified":"2018-10-23T00:12:20","modified_gmt":"2018-10-23T00:12:20","slug":"nodejs-with-redis-linux-hint","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/23\/nodejs-with-redis-linux-hint\/","title":{"rendered":"NodeJS with Redis | Linux Hint"},"content":{"rendered":"<p>Redis is widely used as a caching server. At times, Redis is used as a database as well. It stores the data in a computer\u2019s memory (RAM) directly. The advantage is that it can access the data very fast. The disadvantage is that the data it stores is temporary. If you reboot your computer, then all the data will be gone.<\/p>\n<p>In this article, I will show you how to use Redis with Node.js. I will be using Node.js 10.x on Debian 9 Stretch in this article. But any modern version of Node.js should work. So, let\u2019s get started. To get started you must have the following requirements:<\/p>\n<ul>\n<li>js and NPM installed on your computer.<\/li>\n<li>Redis installed on your computer.<\/li>\n<\/ul>\n<p>You should be able to find articles on installing Node.js and NPM on your desired Linux distribution on LinuxHint.com. I\u2019ve written a dedicated article on <a href=\"https:\/\/linuxhint.com\/install_redis_ubuntu\/\">installing Redis on Ubuntu\/Debian<\/a>.<\/p>\n<h2>Starting Redis:<\/h2>\n<p>You can check whether redis service is running with the following command:<\/p>\n<p>$ sudo systemctl status redis<\/p>\n<p>As you can see, redis service is running.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/image\/gif;base64,R0lGODdhAQABAPAAAP\/wAAACwAAAAAAQABAEACAkQBADs=\" alt=\"\" width=\"1006\" height=\"271\" \/><\/p>\n<p>If redis service is not running in your case, start it with the following command:<\/p>\n<p>$ sudo systemctl start redis<\/p>\n<h3>Initializing the Project Directory:<\/h3>\n<p>First, create a project directory (let\u2019s call it node-redis) with the following command:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/image\/gif;base64,R0lGODdhAQABAPAAAP\/wAAACwAAAAAAQABAEACAkQBADs=\" alt=\"\" width=\"996\" height=\"104\" \/><\/p>\n<p>Now navigate to the project directory ~\/node-redis<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/image\/gif;base64,R0lGODdhAQABAPAAAP\/wAAACwAAAAAAQABAEACAkQBADs=\" alt=\"\" width=\"997\" height=\"102\" \/><\/p>\n<p>Now create a package.json file:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/image\/gif;base64,R0lGODdhAQABAPAAAP\/wAAACwAAAAAAQABAEACAkQBADs=\" alt=\"\" width=\"1018\" height=\"384\" \/><\/p>\n<h3>Installing Redis Node.js Module:<\/h3>\n<p>There are many Redis clients for Node.js. The official website of Redis recommends redis. You can easily install the redis Node.js module using NPM (Node Package Manager).<\/p>\n<p>To install redis Node.js module, run the following NPM command:<\/p>\n<p>$ npm install redis &#8211;save<\/p>\n<p>redis Node.js module should be installed.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/image\/gif;base64,R0lGODdhAQABAPAAAP\/wAAACwAAAAAAQABAEACAkQBADs=\" alt=\"\" width=\"990\" height=\"248\" \/><\/p>\n<h3>Connecting to Redis using Node.js:<\/h3>\n<p>In this section, I am going to show you how to connect to Redis data store using Node.js.<\/p>\n<p>First, create a new file connect.js in your project directory and type in the following commands in it:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/image\/gif;base64,R0lGODdhAQABAPAAAP\/wAAACwAAAAAAQABAEACAkQBADs=\" alt=\"\" width=\"988\" height=\"400\" \/><\/p>\n<p>Here, line 1 imports the redis module.<\/p>\n<p>Line 2 creates a Redis client. As I am running Redis on the same computer as the Node.js programs are running, I didn\u2019t have to specify the hostname or IP address and port where the Redis server is running. If you\u2019re running Redis server on a different computer or server, then you will have to specify it here.<\/p>\n<p>For example, let\u2019s say, your Redis server is running on port 6379 on a computer which has the IP address 192.168.10.87, then you would write line 2 as:<\/p>\n<p>let client = redis.createClient(6379, &#8216;192.168.10.87&#8217;);<\/p>\n<p>Line 4-6 is used to print a message to the console if we can connect to the Redis server.<\/p>\n<p>Line 9-10 is used to print a message to the console if we are unable to connect to the Redis server.<\/p>\n<p>Now, run the connect.js Node.js script as follows:<\/p>\n<p>As you can see, I am connected to the Redis server.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/image\/gif;base64,R0lGODdhAQABAPAAAP\/wAAACwAAAAAAQABAEACAkQBADs=\" alt=\"\" width=\"989\" height=\"96\" \/><\/p>\n<h3>Storing Data in Redis Using Node.js:<\/h3>\n<p>In this section, I will show you how to store data (key-value pairs) in Redis data store with Node.js<\/p>\n<p>First, create set1.js file in your project directory and type in the following lines in it:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/image\/gif;base64,R0lGODdhAQABAPAAAP\/wAAACwAAAAAAQABAEACAkQBADs=\" alt=\"\" width=\"970\" height=\"364\" \/><\/p>\n<p>Here, line 1 and 2 are the same as in connect.js.<\/p>\n<p>On line 4, I set the callback function of the connect event to storeData. So, when our Redis client is connected to the Redis server, the function storeData is called.<\/p>\n<p>On line 6-10, the callback function storeData is defined.<\/p>\n<p>On line 7 and 8, I used set(key, value) method of RedisClient object to set the key name and country to value Mary Smith and USA respectively.<\/p>\n<p>Now, run set1.js as follows:<\/p>\n<p>As you can see, the key-value pairs are set.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/image\/gif;base64,R0lGODdhAQABAPAAAP\/wAAACwAAAAAAQABAEACAkQBADs=\" alt=\"\" width=\"1007\" height=\"126\" \/><\/p>\n<h3>Retrieving Data from Redis Using Node.js<\/h3>\n<p>In this section, I will show you how to retrieve data from Redis data store using Node.js.<\/p>\n<p>First, create a new file get1.js in your project directory and type in the following lines:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/image\/gif;base64,R0lGODdhAQABAPAAAP\/wAAACwAAAAAAQABAEACAkQBADs=\" alt=\"\" width=\"961\" height=\"517\" \/><\/p>\n<p>Here, on line 4, getData function is set as a callback function for the connect event of RedisClient.<\/p>\n<p>On line 6-9, the getData function is defined.<\/p>\n<p>On line 7, I called the get(key, callback) method of RedisClient object. name here is the key of the value you want to retrieve. get() method calls the printValue callback function before it finishes running.<\/p>\n<p>On line 11-18, the error first style callback function printValue() is defined. It accepts 2 arguments, error and result. If any error occurs, then it\u2019s printed on the console and the function exits. If there\u2019s no error, then the value for the certain key is printed on the console.<\/p>\n<p>On line 8, the same thing happens.<\/p>\n<p>Now, run get1.js as follows:<\/p>\n<p>As you can see, the values for the keys name and country is retrieved from the Redis data store.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/image\/gif;base64,R0lGODdhAQABAPAAAP\/wAAACwAAAAAAQABAEACAkQBADs=\" alt=\"\" width=\"1003\" height=\"116\" \/><\/p>\n<h3>Storing Objects in Redis with Node.js:<\/h3>\n<p>You can store JavaScript objects in Redis data store.<\/p>\n<p>First, create a new file set2.js in your project directory and type in the following lines in it.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/image\/gif;base64,R0lGODdhAQABAPAAAP\/wAAACwAAAAAAQABAEACAkQBADs=\" alt=\"\" width=\"943\" height=\"381\" \/><\/p>\n<p>Here, everything is the same as in set1.js file that I already explained earlier. The only difference is, I used client.hmset(key, object) method of RedisClient object in setData() callback function.<\/p>\n<p>In line 7-10, I used client.hmset() method to store a JavaScript object in the Redis data store against the key C011.<\/p>\n<p>Now run set2.js Node.js script as follows:<\/p>\n<p>As you can see, the object is stored.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/image\/gif;base64,R0lGODdhAQABAPAAAP\/wAAACwAAAAAAQABAEACAkQBADs=\" alt=\"\" width=\"998\" height=\"110\" \/><\/p>\n<h3>Retrieving Objects from Redis with Node.js:<\/h3>\n<p>In this section, I am going to show you how to retrieve objects from Redis data store.<\/p>\n<p>Fist, create a new file get2.js in your project directory and type in the following lines in it.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/image\/gif;base64,R0lGODdhAQABAPAAAP\/wAAACwAAAAAAQABAEACAkQBADs=\" alt=\"\" width=\"953\" height=\"368\" \/><\/p>\n<p>Here, everything is the same as in get1.js script I explained earlier.<\/p>\n<p>You can retrieve a JavaScript object very easily with client.hgetall(key, callback) method of RedisClient as in line 7-9.<\/p>\n<p>Now run get2.js Node.js script as follows:<\/p>\n<p>As you can see, the JavaScript object is retrieved from the Redis data store.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/image\/gif;base64,R0lGODdhAQABAPAAAP\/wAAACwAAAAAAQABAEACAkQBADs=\" alt=\"\" width=\"1000\" height=\"122\" \/><\/p>\n<p>I showed you how to setup Redis with Node.js in this article. Now you should be able to read the redis Node.js module documentation at <a href=\"http:\/\/redis.js.org\">http:\/\/redis.js.org<\/a> and learn more about it. Thanks for reading this article.<\/p>\n<p><a href=\"https:\/\/linuxhint.com\/nodejs_redis\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Redis is widely used as a caching server. At times, Redis is used as a database as well. It stores the data in a computer\u2019s memory (RAM) directly. The advantage is that it can access the data very fast. The disadvantage is that the data it stores is temporary. If you reboot your computer, then &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/23\/nodejs-with-redis-linux-hint\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;NodeJS with Redis | Linux Hint&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1262","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/1262","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=1262"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/1262\/revisions"}],"predecessor-version":[{"id":1264,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/1262\/revisions\/1264"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=1262"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=1262"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=1262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}