Connect to Azure Cache For Redis with NodeJS
Updated format of passing parameters to createClient method
This article only highlights the way to connect to Azure Redis server from NodeJS. For full list of commands and usage please refer to Redis and Microsoft documentation
At the time of this article is published, the version of Redis is 4.0.1
To connect to Azure Redis server using host name and primary key, Microsoft clearly described how to create a Redis client in their Javascript sample .
const client = redis.createClient(6380, hostName,
{auth_pass: azureKey, tls: {servername: hostName}});
However, this works only until Redis version 3.0.0. In the later versions of Redis, the mechanism of passing parameters to createClient has changed. The parameters are now required to be passed in below format.
const client = redis.createClient({
socket: {
host: hostName,
port: 6380,
tls: true
},
password: azureKey,
});
It is also required to call connect method on the client object. Otherwise the connection will not be established
client.connect();