DEV Community

vinayak
vinayak

Posted on • Originally published at itsvinayak.hashnode.dev on

4 2

Redis For Javascript | Redis clients and connecting to Redis | part 2

Now that we have written our first javascript program to connect to Redis and save some data to Redis. Time to move forward and understand significant bits and bytes of Redis client and different ways to connect to Redis server.

Redis clients overview

To connect our application to Redis instances, we Redis client or Redis client library that is supported by our applications language. Redis clients have important features such as Managing Redis connection, Implementing Redis protocols, and providing language-specified API for Redis commands.

redis.png

Redis clients use RESP (REdis Serialization Protocol) to communicate with the Redis server. RESP serializes different data types like integers, strings, and arrays, and then sends a Request to the Redis server in form of arrays of strings that represent the command to execute. For this request Redis server replies with command specified data type. RESP is a binary-safe Protocol which means input is treated as a raw stream of bytes and its textual aspect is ignored.

list of different Redis clients

Connecting to Redis from Node.js Application

  • Connecting to Redis using Host and Port


const redis = require('redis');

const connectWithPortAndHost = (port, host) => {
  const client = redis.createClient(
    {
      host: host,
      port: port
    }
  );

  client.connect();
  client.on('connect', () => console.log('connected to redis'));

  client.PING().then((res) => {
    console.log('PING : ', res)
  }).then(() => {
    client.quit();
  });
};

connectWithPortAndHost('6379', '127.0.0.1');



Enter fullscreen mode Exit fullscreen mode

output:



connected to redis
PING : PONG



Enter fullscreen mode Exit fullscreen mode
  • Connecting to Redis with default configurations


const redis = require('redis');

const connectWithDefaultPortAndHost = () => {
  const client = redis.createClient();

  client.connect();
  client.on('connect', () => console.log('connected to redis'));

  client.PING().then((res) => {
    console.log('PING : ', res)
  }).then(() => {
    client.quit();
  });
};

connectWithDefaultPortAndHost();



Enter fullscreen mode Exit fullscreen mode

output:



connected to redis
PING : PONG



Enter fullscreen mode Exit fullscreen mode
  • Connecting to Redis with Redis URL


const redis = require('redis');

const connectWithRedisURL = () => {
  const client = redis.createClient({
    url: 'redis://127.0.0.1:6379',
  });

  client.connect();
  client.on('connect', () => console.log('connected to redis'));

  client.PING().then((res) => {
    console.log('PING : ', res)
  }).then(() => {
    client.quit();
  });
};

connectWithRedisURL();



Enter fullscreen mode Exit fullscreen mode

output:



connected to redis
PING : PONG



Enter fullscreen mode Exit fullscreen mode
  • Connecting to Redis With Password


const redis = require('redis');

const connectWithRedisUsingPassword = (host, port, password) => {
  const client = redis.createClient({
    host: host,
    port: port,
    password: password
  });

  client.connect();
  client.on('connect', () => console.log('connected to redis'));

  client.PING().then((res) => {
    console.log('PING : ', res)
  }).then(() => {
    client.quit();
  });
};

connectWithRedisUsingPassword('localhost', 6379, '123456');



Enter fullscreen mode Exit fullscreen mode

output:



connected to redis
PING : PONG



Enter fullscreen mode Exit fullscreen mode

node-redis library events

In the above code examples, we have used different events like connect and error. There are other different events that tell us about the state of connections.

Example:



client.on('connect' , () => console.log('connect'));
client.on('ready' , () => console.log('ready'));
client.on('reconnecting', () => console.log('reconnecting'));
client.on('error' , () => console.log('error'));
client.on('end' , () => console.log('end'));



Enter fullscreen mode Exit fullscreen mode
node-redis events connection state
ready connection has been made to Redis and is ready to send commands
end connection has been closed
reconnecting retrying to make the connection back
error some error occurred while trying to connect

Connection Pooling in Redis

Connection Pooling is a technic in which an application creates multiple connections with the server or set of connections. Which can be used as needed, once the task is complete connection returns back to the pool.

In the case of nodeJS, it's effectively a single-threaded environment, so connection pooling does not provide performance benefits

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more