Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ app.configure(sync.redis({

// Configure Redis using an existing redisClient
app.configure(sync.redis({
redisClient: redisClient
redisClient: redisClient
}))
```

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

### AMQP

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

## Caveat: Listening to service events

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.
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.

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).

Expand Down
18 changes: 13 additions & 5 deletions lib/adapters/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@ module.exports = config => {
const { key, serialize, deserialize } = config;
const db = config.uri || config.db;
const { redisClient } = config;
// NOTE: message_buffer (redis) and messageBuffer (ioredis) return buffers
const subscriberEvent = config.subscriberEvent || 'message';

if (!redisClient && typeof db !== 'undefined') { debug(`Setting up Redis client for db: ${db}`); }

const pub = redisClient || redis.createClient(db, config.redisOptions);
const sub = redisClient || redis.createClient(db, config.redisOptions);
if (!redisClient && typeof db !== 'undefined') {
debug(`Setting up Redis client for db: ${db}`);
}

const [pub, sub] = redisClient
? [redisClient, redisClient.duplicate()]
: [
redis.createClient(db, config.redisOptions),
redis.createClient(db, config.redisOptions)
];

app.configure(core);
app.sync = {
Expand All @@ -32,7 +40,7 @@ module.exports = config => {
});

sub.subscribe(key);
sub.on('message', function (e, data) {
sub.on(subscriberEvent, function (e, data) {
if (e.toString() === key) {
debug(`Got ${key} message from Redis`);
app.emit('sync-in', data);
Expand Down