xxxxxxxxxx
To connect our application to Redis instances, we Redis client or Redis client library that is supported by our application’s language.
Redis clients have important features such as Managing Redis connection, Implementing Redis protocols, and providing language-specified API for Redis commands.
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.
xxxxxxxxxx
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();
xxxxxxxxxx
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');