This hook connects to Redis and exposes the connection as a global service through strapi. It uses the ioredis package
This hook is compatible with the stable version (3.x) of Strapi only.
$ yarn add strapi-hook-redis-connection
# or
$ npm i strapi-hook-redis-connection --save
To configure the hook edit or add the ./config/hook.js
file in your Strapi app. The config
object is passed to ioredis directly, consult ioredis docs for more info.
module.exports = ({ env }) => ({
timeout: 2000,
settings: {
'redis-connection': {
enabled: false,
config: {
host: env('REDIS_HOST', '127.0.0.1'), // Redis host
port: env.int('REDIS_PORT', 6379), // Redis port
password: env('REDIS_PASSWORD', '')
}
}
},
});
Example to publish messages in a controller
module.exports = {
index: async (ctx) => {
const redis = strapi.services.redis;
if (ctx.request.body.model && ctx.request.body.event) {
redis.publish(ctx.request.body.model, ctx.request.body.event);
}
// Send 200 `ok`
ctx.send({
message: 'ok'
});
},
};