How to use the ioredis.Cluster function in ioredis

To help you get started, we’ve selected a few ioredis examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github lawrips / redis-live / index.js View on Github external
var commands = require('./lib/modules/commands');


app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars'); 
app.use(express.static('lib/public'));
app.use( bodyParser.json() );       // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({     // to support URL-encoded bodies
  extended: true
})); 
console.log(nconf.get('cluster'))

app.listen(9999);

let cluster = new Redis.Cluster(nconf.get('cluster'), 
{
    redisOptions: nconf.get('options')
});

app.get('/', (req, res) => {
    let hosts = nconf.get('cluster').map((i) => i.host + ':' + i.port);
    return res.render('index', {commands: JSON.stringify(commands.get()), hosts: JSON.stringify(hosts)});
});

app.post('/info', (req, res) => {
    let redisOptions = nconf.get('options');
    redisOptions.host = req.body.host.split(':')[0];
    redisOptions.port = req.body.host.split(':').length > 1 ? req.body.host.split(':')[1] : 6379;
    let redis = new Redis(redisOptions);

    redis.info((err, result) => {
github coralproject / talk / services / redis.js View on Github external
const createClient = () => {
  let client;
  if (REDIS_CLUSTER_MODE === 'NONE') {
    client = new Redis(
      REDIS_URL,
      merge({}, REDIS_CLIENT_CONFIG, {
        retryStrategy,
      })
    );
  } else if (REDIS_CLUSTER_MODE === 'CLUSTER') {
    client = new Redis.Cluster(
      REDIS_CLUSTER_CONFIGURATION,
      merge(
        {
          scaleReads: 'slave',
        },
        REDIS_CLIENT_CONFIG,
        {
          clusterRetryStrategy: retryStrategy,
        }
      )
    );
  }

  // Attach the monitors that will print debug messages to the console.
  attachMonitors(client);
github ruojs / ruo / src / ws.js View on Github external
function createWebSocketApplication (server, api, options) {
  if (!options) {
    return
  }

  const io = require('socket.io')(server, {path: options.path})
  const wsapp = express()
  wsapp.io = io
  wsapp.extendMiddleware = extendMiddleware

  if (options.session) {
    if (options.session.redis) {
      const redis = options.session.redis
      // pub and sub shuold not use the same instance
      const pubClient = Array.isArray(redis) ? new Redis.Cluster(redis) : new Redis(redis)
      const subClient = Array.isArray(redis) ? new Redis.Cluster(redis) : new Redis(redis)
      io.adapter(ioRedis({
        key: `${rc.name}:socket.io`,
        pubClient: pubClient,
        subClient: subClient,
        subEvent: 'messageBuffer'
      }))
    }
    io.use(ioSession(createSession(options.session)))
  }

  io.on('connection', (socket) => {
    const session = socket.handshake.session
    // join sid and userId to allow send message to particular socket
    socket.join(`session ${session.id}`)
    // TODO: custom userId field?
    socket.join(`user ${session.userId}`)
github kube-HPC / hkube / core / pipeline-driver / tests.js View on Github external
function createClient() {
            return config.redis.useCluster ? new Redis.Cluster([config.redis]) : new Redis(config.redis)
        }
github liutian / light-push / src / util / redis-factory.js View on Github external
function create() {
  let connection;

  if (Array.isArray(config.redis_address)) {
    var addressArr = config.redis_address.map(function (item) {
      return { host: item.split(':')[0], port: item.split(':')[1] };
    });

    var options = {
      keyPrefix: config.redis_prefix,
      password: config.redis_password
    };

    connection = new Redis.Cluster(addressArr, options);
  } else {
    var options = {
      host: config.redis_address.split(':')[0],
      port: config.redis_address.split(':')[1],
      keyPrefix: config.redis_prefix,
      password: config.redis_password
    };

    connection = new Redis(options);
  }

  connection.on('error', function (e) {
    logger.error('redis connection fail ' + e);
  });
  connection.on('connect', function () {
    logger.log('redis connection ready');
github auth0 / node-baas / lib / db / redis.js View on Github external
module.exports = function (dbOptions) {
  var redis;

  if (Array.isArray(dbOptions.nodes)) {
    redis = new Redis.Cluster(dbOptions.nodes, _.omit(dbOptions, ['nodes']));
  } else {
    redis = new Redis(dbOptions);
  }

  var redlock = new Redlock(
    [redis],
    {
      retryCount:  300,
      retryDelay:  5
    }
  );

  return {
    create: function (bucket_name) {
      return new BucketDB(bucket_name, redis, redlock);
    }
github benjsicam / node-graphql-microservices / grpc-services / comments / src / main.js View on Github external
let cache
  let redisOptions = {}

  if (redisHostConfig.length > 1) {
    const redisNodes = map(redisHostConfig, host => {
      return {
        host,
        port: process.env.REDIS_PORT
      }
    })

    redisOptions = {
      password: process.env.REDIS_PASSWORD
    }

    cache = new Redis.Cluster(redisNodes, {
      slotsRefreshTimeout: 20000,
      redisOptions
    })
  } else {
    redisOptions = {
      host: process.env.REDIS_HOST,
      port: process.env.REDIS_PORT,
      password: process.env.REDIS_PASSWORD
    }

    cache = new Redis(redisOptions)
  }

  const cacheService = new CacheService(cache, logger)
  const cacheMiddleware = new CacheMiddleware(cacheService, logger)
github thx / rap2-delos / src / service / redis.ts View on Github external
import * as redis from 'redis'
import config from '../config'
import * as ioredis from 'ioredis'

export enum CACHE_KEY {
  REPOSITORY_GET = 'REPOSITORY_GET',
}

export default class RedisService {
  private static client: redis.RedisClient = config.redis && config.redis.isRedisCluster ? new ioredis.Cluster(config.redis.nodes, {redisOptions: config.redis.redisOptions}) : redis.createClient(config.redis)

  private static getCacheKey(key: CACHE_KEY, entityId?: number): string {
    return `${key}:${entityId || ''}`
  }

  public static getCache(key: CACHE_KEY, entityId?: number): Promise {
    const cacheKey = this.getCacheKey(key, entityId)
    return new Promise((resolve, reject) => {
      RedisService.client.get(cacheKey, (error: Error, value: string | null) => {
        if (error) {
          return reject(error)
        }
        resolve(value)
      })
    })
  }
github benjsicam / node-graphql-microservices / graphql-gateway / src / main.js View on Github external
return {
        host,
        port: process.env.REDIS_PORT
      }
    })

    redisOptions = {
      password: process.env.REDIS_PASSWORD,
      keyPrefix: process.env.NODE_ENV
    }

    publisher = new Redis.Cluster(redisNodes, {
      slotsRefreshTimeout: 20000,
      redisOptions
    })
    subscriber = new Redis.Cluster(redisNodes, {
      slotsRefreshTimeout: 20000,
      redisOptions
    })
  } else {
    redisOptions = {
      host: process.env.REDIS_HOST,
      port: process.env.REDIS_PORT,
      password: process.env.REDIS_PASSWORD,
      keyPrefix: process.env.NODE_ENV
    }

    publisher = new Redis(redisOptions)
    subscriber = new Redis(redisOptions)
  }

  const pubsub = new RedisPubSub({

ioredis

A robust, performance-focused and full-featured Redis client for Node.js.

MIT
Latest version published 7 days ago

Package Health Score

98 / 100
Full package analysis