How to use the createClient function from redis

Find comprehensive JavaScript redis.createClient code examples handpicked from public code repositorys.

redis.createClient is a function in the Redis client library that creates a new client instance for connecting to a Redis server.

163
164
165
166
167
168
169
170
171
172
  t.end()
})

// Simulate a redis client error with `enable_offline_queue: false` and a
// quick `.set()` before the client connection ready.
var client = redis.createClient({
  host: process.env.REDIS_HOST,
  port: '6379',
  enable_offline_queue: false
})
fork icon211
star icon536
watch icon296

+ 3 other calls in file

9
10
11
12
13
14
15
16
17
18
19
20
21
const zoro = new ANIME.Zoro();


let redisClient;


(async () => {
    redisClient = redis.createClient({
        url: process.env.REDIS_URL,
    });


  redisClient.on("error", (error) => console.error(`Error : ${error}`));
fork icon23
star icon72
watch icon7

How does redis.createClient work?

redis.createClient is a function provided by the Redis client library for Node.js that creates a new client instance for connecting to a Redis server.

When called, redis.createClient establishes a new connection to the Redis server and returns a new RedisClient instance that can be used to send commands to the server. The function takes an optional configuration object as its argument, which can be used to set options such as the Redis server host and port, the client's authentication credentials, and other parameters.

Once a RedisClient instance has been created using redis.createClient, developers can use the client's methods to send commands to the Redis server. For example, to set a key-value pair in the Redis server, developers can call the set method on the client:

javascript
const redis = require('redis'); const client = redis.createClient(); client.set('mykey', 'myvalue', (error, result) => { if (error) { console.error(error); } else { console.log(result); } });

In this example, we create a new Redis client using redis.createClient with the default options. We then call the client's set method to set a key-value pair in the Redis server with the key "mykey" and the value "myvalue". The set method takes a callback function as its last argument, which is called with an error or result depending on the success of the operation.

By using redis.createClient and other methods provided by the Redis client library, developers can easily interact with Redis servers from their Node.js applications, making it a valuable tool for many server-side projects.

310
311
312
313
314
315
316
317
318
319
const { createClient, RedisClientType } = require('redis');

async function _doWork(){
  try {
    const db = await new Promise((resolve, reject) => {
      const client = createClient(${JSON.stringify(clientOptions)});
      client.connect();
      client.on('ready', () => resolve(client));
      client.on('error', (err) => reject(err));
    });
fork icon7
star icon49
watch icon4

-1
fork icon5
star icon4
watch icon3

+ 12 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const redis = require("redis");
const client = redis.createClient();

client.on("error", (error) => {
  console.error(error);
});

client.set("mykey", "myvalue", (error, result) => {
  if (error) {
    console.error(error);
  } else {
    console.log(result);
  }
});

client.quit();

In this example, we create a new Redis client using redis.createClient with the default options. We then set up an error listener on the client using the on method, which logs any errors that occur during communication with the Redis server. Next, we call the client's set method to set a key-value pair in the Redis server with the key "mykey" and the value "myvalue". The set method takes a callback function as its last argument, which is called with an error or result depending on the success of the operation. Finally, we call the client's quit method to gracefully shut down the connection to the Redis server. By using redis.createClient and other methods provided by the Redis client library, developers can easily interact with Redis servers from their Node.js applications, making it a valuable tool for many server-side projects.

17
18
19
20
21
22
23
24
25
26
}
var self = this;
var deferred = getDefer();
var port = this.config.redis_port || '6379';
var host = this.config.redis_host || '127.0.0.1';
var connection = redis.createClient(port, host, this.config);
if (this.config.redis_password) {
    connection.auth(this.config.redis_password, function () {
    });
}
fork icon3
star icon1
watch icon2

+ 7 other calls in file

0
1
2
3
4
5
6
7
8
9
const redis = require('redis');
const config = require('./config.json').redis;
const redisKey = require('./redisKey');
const log = require('./log4jsUtil');
const redisUtil = {};
const client = redis.createClient(config.port, config.host, {auth_pass: config.password});

client.on('error',function (err) {
    log.error('redis error:'+err);
});
fork icon2
star icon23
watch icon1

+ 13 other calls in file

117
118
119
120
121
122
123
124
125
126

npm 包 [redis](https://www.npmjs.com/package/redis) 能满足我们的全部需求。先看连接数据库:

```js
const redis = require('redis');
const client = redis.createClient();
client.on('ready', function () {
    console.log('Redis 连接成功:');
});
```
fork icon0
star icon20
watch icon4

+ 3 other calls in file

39
40
41
42
43
44
45
46
47

// connect to Redis server
function* _connectRedisServer(dConfig) {
  const password = dConfig.password || 'thepasswordisme';
  const db_id = dConfig.name;
  let db = redis.createClient(dConfig.port, dConfig.host, {});
  db.on("error", (err) => {
    mbgGame.logError(`[REDIS <${db_id}> ][Error] `, err);
  });
fork icon0
star icon3
watch icon2

+ 13 other calls in file

-3
fork icon13
star icon0
watch icon2

+ 13 other calls in file

20
21
22
23
24
25
26
27
28
29
tronWeb.setHeader({"TRON-PRO-API-KEY": tronWebAPiKey});


const redis = require('redis');

var redisClient = redis.createClient(redis_port,redis_host);
if(redis_pass){
    redisClient.auth(redis_pass); 
}
redisClient.select(15);
fork icon4
star icon0
watch icon0

+ 13 other calls in file

2
3
4
5
6
7
8
9
10
11
12
13
14


let client;


exports.connectToRedis = async function () {
  const { redis } = getConfig();
  client = createClient(redis);


  await client.connect();
};

fork icon3
star icon16
watch icon0

22
23
24
25
26
27
28
29
30
31
} = require('../utils/helper-ee');
const {createAdapter} = require("@socket.io/redis-adapter");
const {createClient} = require("redis");
const wsRouter = express.Router();
const REDIS_URL = (process.env.REDIS_URL || "localhost:6379").replace(/((^\w+:|^)\/\/|^)/, 'redis://');
const pubClient = createClient({url: REDIS_URL});
const subClient = pubClient.duplicate();
console.log(`Using Redis: ${REDIS_URL}`);
let io;
const debug = process.env.debug === "1";
fork icon339
star icon0
watch icon67

+ 9 other calls in file

803
804
805
806
807
808
809
810
811
812
813
    }
  })
}


function connectRedis() {
  redis_client = redis.createClient({
    host: REDIS_HOST,
    port: REDIS_PORT,
    password: REDIS_PASSWORD
  });
fork icon3
star icon6
watch icon1

+ 8 other calls in file

-1
fork icon2
star icon0
watch icon2

+ 55 other calls in file

26
27
28
29
30
31
32
33
34
35
var token = md5.digest('hex');
console.log('tokenGame: ',token);


var clientGame = redis.createClient(8359, '10.16.33.62'),
    clientDashboard = redis.createClient(8359, '10.16.33.62');
clientGame.auth(optionsGame[0]+':'+optionsGame[1]);
clientDashboard.auth(optionsDashboard[0]+':'+optionsDashboard[1]);

clientGame.subscribe('@');
fork icon0
star icon4
watch icon8

+ 7 other calls in file

107
108
109
110
111
112
113
114
115
116

```javascript
const redis = require('redis');
const Promise = require('bluebird');
Promise.promisifyAll(redis.RedisClient.prototype);
let client = redis.createClient({
  url: 'redis://redistogo:YOUR_PASSWORD@koi.redistogo.com:11517'
});
client.randomkeyAsync()
  .then(key => {
fork icon0
star icon3
watch icon8

+ 11 other calls in file

73
74
75
76
77
78
79
80
81
82
const log = logger.child({ redis: name })
// let isReady = false

const defaultConfig = _getDefaultRedisOptions(name)
const config = { ...defaultConfig, ...options }
const client = redis.createClient(config)

const callbackOnce = _once(callback)
Global.clients[name] = client
Global.clients[name].log = log
fork icon0
star icon2
watch icon0

+ 15 other calls in file

-2
fork icon0
star icon2
watch icon0

+ 2 other calls in file

384
385
386
387
388
389
390
391
392
393
394
395
        });
    });
}


// connect to Redis
var redisClient = redis.createClient({
    host: redisHost
});


redisClient.on('error', (e) => {
fork icon15
star icon1
watch icon1