Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add ioredis dep, test #191

Open
wants to merge 2 commits into
base: release
Choose a base branch
from
Open
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
32 changes: 18 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ When running multiple instances of your Feathers application (e.g. on several He

feathers-sync uses a messaging mechanism to propagate all events to all application instances. It currently supports:

- Redis via [redis](https://github.com/NodeRedis/node_redis)
- Redis via [ioredis](https://github.com/luin/ioredis)
- AMQP (RabbitMQ) via [amqplib](https://github.com/squaremo/amqp.node)

This allows to scale real-time websocket connections to any number of clients.
Expand Down Expand Up @@ -94,33 +94,39 @@ app.service('messages').hooks({
`feathers-sync` can be initialized either by specifying the type of adapter through the `uri` (e.g. `redis://localhost:6379`) or using e.g. `sync.redis` directly:

```js
// Configure Redis
// Configure with Redis
app.configure(
sync({
uri: 'redis://localhost:6379',
})
);

app.configure(
sync.redis({
db: redisInstance,
})
);
// Configure Redis using an existing redis instance
const Redis = require('ioredis');
const redisClient = new Redis({
// see https://luin.github.io/ioredis/index.html#RedisOptions for additional options
host: 'my.redis.host.com',
port: 6379,
lazyConnect: true,
maxRetriesPerRequest: null,
// ...
});

// Configure Redis using an existing redisClient
app.configure(
sync.redis({
redisClient: redisClient,
key: 'feathers-sync',
serialize: JSON.stringify,
deserialize: JSON.parse,
redisClient
})
);
```

### Redis

- `uri` - The connection string (must start with `redis://`)
- `uri` - The connection string (must start with `redis://`) (default: `localhost:6379`)
- `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)
- `redisClient` - (optional) An existing instance of a redis client

### AMQP

Expand Down Expand Up @@ -155,8 +161,6 @@ const bson = require('bson');
app.configure(
sync({
uri: 'redis://localhost:6379',
// Replies will be sent to callbacks as Buffers instead of Strings for bson.deserialize to work.
redisOptions: { return_buffers: true },
serialize: bson.serialize,
deserialize: bson.deserialize,
})
Expand Down
27 changes: 12 additions & 15 deletions lib/adapters/redis.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
const redis = require('redis');
const Redis = require('ioredis');
const debug = require('debug')('feathers-sync:redis');
const core = require('../core');

module.exports = config => {
return app => {
const { key, serialize, deserialize, redisClient, uri } = config;
const options = {
url: uri,
...config.redisOptions
};

if (!redisClient) {
debug(`Setting up Redis client for ${options.uri}`);
debug(`Setting up new Redis client for ${uri}`);
}

const pub = redisClient || redis.createClient(options);
const pub = redisClient || new Redis(uri);
const sub = pub.duplicate();

const msgFromRedisHandler = data => {
debug(`Got ${key} message from Redis`);
app.emit('sync-in', data);
};

app.configure(core);
app.sync = {
deserialize,
Expand All @@ -30,11 +21,17 @@ module.exports = config => {
sub,
type: 'redis',
ready: new Promise((resolve, reject) => {
pub.connect();
sub.connect();
sub.once('ready', resolve);
sub.once('error', reject);
}).then(() => sub.subscribe(key, msgFromRedisHandler, true))
}).then(() => {
sub.subscribe(key);
sub.on(serialize && deserialize ? 'messageBuffer' : 'message', function (channel, data) {
if (channel.toString() === key) {
debug(`Got ${key} message from Redis`);
app.emit('sync-in', data);
}
});
})
};

app.on('sync-out', data => {
Expand Down
Loading