Skip to content

Allow to register multiple instances of Redis client. #28

@darkgl0w

Description

@darkgl0w

🚀 Feature Proposal

Allow to register multiple instances of Redis client.

Motivation

This feature will make it easier to use Redis features such as Pub/Sub with fastify-redis.

Example

We register 2 different clients giving each one a distinct namespace:

const fastify = require('fastify')
const redis = require('redis').createClient({ host: 'localhost', port: 6379 })

fastify
  .register(require('fastify-redis'), {
    host: '127.0.0.1',
    namespace: 'subscriber'
  })
  .register(require('fastify-redis'), {
    client: redis,
    namespace: 'publisher'
  })

// ...

Then we can do something like this in our application :

// ...
const channel = 'fastifyIsAwesome'

fastify.redis.subscriber.subscribe(channel, (error, count) => {
  if (error) {
    throw new Error(error)
  }

  console.log(`Subscribed to ${count} channel. Listening for updates on the '${channel}' channel.`)
})

fastify.redis.subscriber.on('message', (channel, message) => {
  console.log(`We received the following message from '${channel}': ${message}`)
})

fastify.redis.publisher.publish(channel, 'hello');

// We would have a console output like this :

// Subscribed to 1 channel. Listening for updates on the 'fastifyIsAwesome' channel.
// We received the following message from 'fastifyIsAwesome': hello
// We received the following message from 'fastifyIsAwesome': world

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions