Skip to content

Commit 37bbbc4

Browse files
authored
Duplicate redisClient (bug fix) * Customize subscriber event (#158)
1 parent 51a43ba commit 37bbbc4

2 files changed

Lines changed: 16 additions & 7 deletions

File tree

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ app.configure(sync.redis({
8585

8686
// Configure Redis using an existing redisClient
8787
app.configure(sync.redis({
88-
redisClient: redisClient
88+
redisClient: redisClient
8989
}))
9090
```
9191

@@ -96,6 +96,7 @@ app.configure(sync.redis({
9696
- `key` - The key under which all synchronization events will be stored (default: `feathers-sync`)
9797
- `redisClient` - An existing instance of redisClient
9898
- `redisOptions` - Redis [client options](http://redis.js.org/#api-rediscreateclient)
99+
- `subscriberEvent` - The event to listen for. Defaults to `message`. Could be `message_buffer` or `messageBuffer` depending on what Redis library is being used.
99100

100101
### AMQP
101102

@@ -115,7 +116,7 @@ app.configure(sync.redis({
115116

116117
## Caveat: Listening to service events
117118

118-
With `feathers-sync` enabled all events are going to get propagated to every application instance. This means, that any event listeners registered _on the server_ should not perform any actions that change the global state (e.g. write something into the database or call to an external API) because it will end up running multiple times (once on each instance). Instead, event listeners should only be used to update the local state (e.g. a local cache) and send real-time updates to all its clients.
119+
With `feathers-sync` enabled all events are going to get propagated to every application instance. This means, that any event listeners registered _on the server_ should not perform any actions that change the global state (e.g. write something into the database or call to an external API) because it will end up running multiple times (once on each instance). Instead, event listeners should only be used to update the local state (e.g. a local cache) and send real-time updates to all its clients.
119120

120121
If you need to perform actions, for example setting up a first blog post after a new user has been created, add it to the service method itself or use a [Feathers hook](https://docs.feathersjs.com/api/hooks.html) (both of which will only run once on the instance that is handling the request).
121122

lib/adapters/redis.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,19 @@ module.exports = config => {
77
const { key, serialize, deserialize } = config;
88
const db = config.uri || config.db;
99
const { redisClient } = config;
10+
// NOTE: message_buffer (redis) and messageBuffer (ioredis) return buffers
11+
const subscriberEvent = config.subscriberEvent || 'message';
1012

11-
if (!redisClient && typeof db !== 'undefined') { debug(`Setting up Redis client for db: ${db}`); }
12-
13-
const pub = redisClient || redis.createClient(db, config.redisOptions);
14-
const sub = redisClient || redis.createClient(db, config.redisOptions);
13+
if (!redisClient && typeof db !== 'undefined') {
14+
debug(`Setting up Redis client for db: ${db}`);
15+
}
16+
17+
const [pub, sub] = redisClient
18+
? [redisClient, redisClient.duplicate()]
19+
: [
20+
redis.createClient(db, config.redisOptions),
21+
redis.createClient(db, config.redisOptions)
22+
];
1523

1624
app.configure(core);
1725
app.sync = {
@@ -32,7 +40,7 @@ module.exports = config => {
3240
});
3341

3442
sub.subscribe(key);
35-
sub.on('message', function (e, data) {
43+
sub.on(subscriberEvent, function (e, data) {
3644
if (e.toString() === key) {
3745
debug(`Got ${key} message from Redis`);
3846
app.emit('sync-in', data);

0 commit comments

Comments
 (0)