2016-06-21 08:28:26 +08:00
|
|
|
const bluebird = require('bluebird')
|
|
|
|
const redis = require("redis");
|
|
|
|
bluebird.promisifyAll(redis.RedisClient.prototype);
|
|
|
|
bluebird.promisifyAll(redis.Multi.prototype);
|
|
|
|
|
|
|
|
class DeltaStreamQueue {
|
|
|
|
setup() {
|
2016-06-22 08:10:34 +08:00
|
|
|
this.client = redis.createClient(process.env.REDIS_URL);
|
2016-06-21 08:28:26 +08:00
|
|
|
this.client.on("error", console.error);
|
|
|
|
this.client.on("ready", () => console.log("Redis ready"));
|
|
|
|
}
|
|
|
|
|
|
|
|
key(accountId) {
|
|
|
|
return `delta-${accountId}`
|
|
|
|
}
|
|
|
|
|
2016-06-22 06:57:50 +08:00
|
|
|
notify(accountId, data) {
|
|
|
|
this.client.publish(this.key(accountId), JSON.stringify(data))
|
2016-06-21 08:28:26 +08:00
|
|
|
}
|
|
|
|
|
2016-06-22 06:57:50 +08:00
|
|
|
subscribe(accountId, callback) {
|
|
|
|
this.client.on("message", (channel, message) => {
|
|
|
|
if (channel !== this.key(accountId)) { return }
|
|
|
|
callback(message)
|
2016-06-21 08:28:26 +08:00
|
|
|
})
|
2016-06-22 06:57:50 +08:00
|
|
|
this.client.subscribe(this.key(accountId))
|
2016-06-21 08:28:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = new DeltaStreamQueue()
|