https://github.com/node-casbin/redis-watcher
Redis watcher for Node-Casbin
https://github.com/node-casbin/redis-watcher
abac acl auth authorization authz casbin js node nodejs rbac redis watcher
Last synced: 28 days ago
JSON representation
Redis watcher for Node-Casbin
- Host: GitHub
- URL: https://github.com/node-casbin/redis-watcher
- Owner: node-casbin
- License: apache-2.0
- Created: 2019-07-07T08:09:19.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-03-08T04:37:06.000Z (almost 2 years ago)
- Last Synced: 2025-10-19T04:57:28.704Z (4 months ago)
- Topics: abac, acl, auth, authorization, authz, casbin, js, node, nodejs, rbac, redis, watcher
- Language: TypeScript
- Homepage: https://github.com/casbin/node-casbin
- Size: 350 KB
- Stars: 7
- Watchers: 2
- Forks: 8
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Redis Watcher
[](https://github.com/node-casbin/redis-watcher/actions/workflows/ci.yml)
[](https://coveralls.io/github/node-casbin/redis-watcher?branch=master)
[![NPM version][npm-image]][npm-url]
[![NPM download][download-image]][download-url]
[](https://discord.gg/S5UjpzGZjN)
[npm-image]: https://img.shields.io/npm/v/@casbin/redis-watcher.svg?style=flat-square
[npm-url]: https://npmjs.com/package/@casbin/redis-watcher
[download-image]: https://img.shields.io/npm/dm/@casbin/redis-watcher.svg?style=flat-square
[download-url]: https://npmjs.com/package/@casbin/redis-watcher
Redis Watcher is the [Redis](https://redis.io/) watcher for [Node-Casbin](https://github.com/casbin/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](https://github.com/luin/ioredis) and supports both single Redis instances and Redis clusters.
## Installation
```bash
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:
```typescript
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:
```typescript
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
- [Node-Casbin](https://github.com/casbin/node-casbin)
- [Casbin Forum](https://forum.casbin.com/)
## License
This project is under Apache 2.0 License. See the [LICENSE](LICENSE) file for the full license text.