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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ app.configure(

## How it works

![alt tag](https://raw.githubusercontent.com/PedroMD/feathers-sync/master/feathers-sync%20and%20real-time%20events-60.png)
![alt tag](https://raw.githubusercontent.com/PedroMD/feathers-sync/master/feathers-sync.png)

## Caveat: Listening to service events

Expand Down Expand Up @@ -215,6 +215,6 @@ The `data` for the `sync-in` event should be in the same form as the one that is

## License

Copyright (c) 2021 Feathers contributors
Copyright (c) 2025 Feathers contributors

Licensed under the [MIT license](LICENSE).
File renamed without changes
71 changes: 43 additions & 28 deletions lib/adapters/redis.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,60 @@
const redis = require('redis')
const debug = require('debug')('feathers-sync:redis')
const core = require('../core')
const redis = require("redis");
const debug = require("debug")("feathers-sync:redis");
const core = require("../core");

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

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

const pub = redisClient || redis.createClient(options)
const sub = pub.duplicate()
const pub = redisClient || redis.createClient(options);
const sub = pub.duplicate();
const errorHandlers = pub.listeners("error");

const msgFromRedisHandler = data => {
debug(`Got ${key} message from Redis`)
app.emit('sync-in', data)
if (errorHandlers.length > 0) {
// If error handlers exists, copy them to sub
errorHandlers.forEach((handler) => {
sub.on("error", handler);
});
} else {
// If not, make sure both pub and sub has an error handler to avoid unhandled rejections
const defaultErrorHandler = (err) => {
console.error("REDIS ERROR", err);
};
pub.on("error", defaultErrorHandler);
sub.on("error", defaultErrorHandler);
}

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

app.configure(core);
app.sync = {
deserialize,
serialize,
pub,
sub,
type: 'redis',
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))
}
pub.connect();
sub.connect();
sub.once("ready", resolve);
sub.once("error", reject);
}).then(() => sub.subscribe(key, msgFromRedisHandler, true)),
};

app.on('sync-out', data => {
debug(`Publishing key ${key} to Redis`)
pub.publish(key, data)
})
}
}
app.on("sync-out", (data) => {
debug(`Publishing key ${key} to Redis`);
pub.publish(key, data);
});
};
};