Skip to content
Closed
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 @@ -88,7 +88,7 @@ app.configure(sync.redis({

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

Expand All @@ -99,6 +99,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 @@ -112,7 +113,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
14 changes: 10 additions & 4 deletions lib/adapters/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ module.exports = config => {
const key = config.key || 'feathers-sync';
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")
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);
// NOTE: duplicate() works with redis and ioredis
const [pub, sub] = redisClient ? [redisClient, redisClient.duplicate()] : [
redis.createClient(db, config.redisOptions),
redis.createClient(db, config.redisOptions)
];

const { deserialize, serialize } = config;

Expand All @@ -35,7 +41,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
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "feathers-sync",
"description": "Feathers",
"version": "2.1.0",
"version": "2.1.1",
"repository": {
"type": "git",
"url": "https://github.com/feathersjs-ecosystem/feathers-sync.git"
Expand Down