Redis watcher for Node-Casbin

Clone this repo:
  1. d3b17c0 chore(release): 1.2.0 [skip ci] by semantic-release-bot · 2 months ago master v1.2.0
  2. 16afbd5 feat: fix CI build by pinning @types/babel__traverse for TypeScript 3.7.4 compatibility (#39) by Yang Luo · 2 months ago
  3. a5f78ed feat: fix bug that NPM released, but github not released (#37) by Yang Luo · 2 months ago
  4. 662c6a7 chore(release): 1.1.0 [skip ci] by semantic-release-bot · 2 months ago v1.1.0
  5. dd63a07 feat: pin casbin version and drop Node.js 18 to fix CI test failures (#34) by Yang Luo · 2 months ago

Redis Watcher

CI Coverage Status NPM version NPM download Discord

Redis Watcher is the Redis watcher for Node-Casbin. With this library, Node-Casbin can synchronize policy changes across multiple enforcer instances via Redis pub/sub.

This watcher is based on ioredis and supports both single Redis instances and Redis clusters.

Installation

npm install @casbin/redis-watcher

Note: The old package name redis-watcher has been deprecated on NPM. Please use @casbin/redis-watcher instead.

Simple Example

Using Redis:

import { RedisWatcher } from '@casbin/redis-watcher';
import { newEnforcer } from 'casbin';

async function myFunction() {
    // Initialize the watcher
    // See https://github.com/luin/ioredis/blob/master/API.md#new-redisport-host-options
    const watcher = await RedisWatcher.newWatcher('redis://localhost:6379/5');

    // Initialize the enforcer
    const enforcer = await newEnforcer('examples/authz_model.conf', 'examples/authz_policy.csv');

    // Set the watcher for the enforcer
    enforcer.setWatcher(watcher);

    // Update the policy
    await enforcer.addPolicy('alice', 'data1', 'read');
    
    // The policy change will be synchronized to other enforcers via Redis pub/sub
}

Using Redis Cluster:

import { RedisWatcher } from '@casbin/redis-watcher';
import { newEnforcer } from 'casbin';

async function myFunction() {
    // Initialize the watcher with Redis cluster
    // See https://github.com/luin/ioredis/blob/master/API.md#new-clusterstartupnodes-options
    const watcher = await RedisWatcher.newWatcherWithCluster([
        { port: 7000, host: 'localhost' },
        { port: 7001, host: 'localhost' },
        { port: 7002, host: 'localhost' }
    ]);

    // Initialize the enforcer
    const enforcer = await newEnforcer('examples/authz_model.conf', 'examples/authz_policy.csv');

    // Set the watcher for the enforcer
    enforcer.setWatcher(watcher);

    // Update the policy
    await enforcer.addPolicy('bob', 'data2', 'write');
    
    // The policy change will be synchronized to other enforcers via Redis pub/sub
}

Getting Help

License

This project is under Apache 2.0 License. See the LICENSE file for the full license text.