如何从节点js中的另一个进程更新网站&&手机?

How to update the website && the mobile from another process in node js?

我有一些流程:

var fetcher = new Fetcher({ url: 'http://www.example.com' });
setInterval(function() {
    fetcher.getImageUrls().then(saveToDb, handleError, notifyProgress);
}, 10000);

我也有一个网站,我想用socket.io推送更新 关于进度(使用 notifyProgress 方法)。我不知道 如何将网站与进程同步。

请注意,我不能使用事件,因为流程可以存在于任何地方(我也可以用更多实例复制流程)

我该怎么做?

谢谢

您想使用 Redis 创建一个 PUB/SUB 系统。

您将向频道发布您的更新;

// notifyProgress.js
var Redis = require('redis'),
    redis = Redis.createClient(6379, 127.0.0.1);

module.exports.publish = function (req, res, next) {
    //publish to the mySpecialChannel channel.
    redis.publish('mySpecialChannel', data);
};

您的另一个 servers/clusters 将订阅该频道。

//socket server
var Redis = require('redis'),
    redis = Redis.createClient(6379, 127.0.0.1),
    app = express(),
    server = require('http').createServer(app),
    io = require('socket.io')(server);

redis.on('subscribe', function (channel, count) {
    console.log('server subscribed to %s', channel);
});

redis.on('message', function (channel, message) {
    if (channel === 'mySpecialChannel') {
        //send info to your clients.
        io.emit('update', message)
    }
});

redis.subscribe('mySpecialChannel');