Skip to content

Commit 5b97544

Browse files
babysealclubberChang Lee
andauthored
Subscribe to Redis messages after Redis client connects. Subscribing before connection causes _RedisCommandsQueue_pubSubState in node-redis 4.x to get set resulting in 'AuthError: Cannot send commands in PubSub mode' error. (#180)
Co-authored-by: Chang Lee <c.lee@servicepower.com>
1 parent 0359e5c commit 5b97544

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

lib/adapters/redis.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ module.exports = config => {
1717
const pub = redisClient || redis.createClient(options);
1818
const sub = pub.duplicate();
1919

20+
const msgFromRedisHandler = data => {
21+
debug(`Got ${key} message from Redis`);
22+
app.emit('sync-in', data);
23+
};
24+
2025
app.configure(core);
2126
app.sync = {
2227
deserialize,
@@ -29,17 +34,12 @@ module.exports = config => {
2934
sub.connect();
3035
sub.once('ready', resolve);
3136
sub.once('error', reject);
32-
})
37+
}).then(() => sub.subscribe(key, msgFromRedisHandler, true))
3338
};
3439

3540
app.on('sync-out', data => {
3641
debug(`Publishing key ${key} to Redis`);
3742
pub.publish(key, data);
3843
});
39-
40-
sub.subscribe(key, data => {
41-
debug(`Got ${key} message from Redis`);
42-
app.emit('sync-in', data);
43-
}, true);
4444
};
4545
};

0 commit comments

Comments
 (0)