5

i'm using node.js for the first time and hoping for an advice:

i installed the following programs on my server:

  • node.js v0.11.3-pre
  • express v3.3.4
  • socket.io v0.9.14
  • connect-redis v1.4.5
  • Redis server v=2.6.14
  • redis-cli 2.6.14

First of all, i created an express app:

express testApplication

In the created "package.json" i defined all neccessary depencies.

From the start i defined a cluster for vertically scaling (multi-processes) in a file called "cluster.js":

var cluster = require('cluster');

if( cluster.isMaster ) {
        var noOfWorkers = process.env.NODE_WORKERS || require('os').cpus().length;

        console.log("Workers found: " + noOfWorkers);

        for (var i = 0; i < noOfWorkers; i += 1) {
                cluster.fork();
        }
} else {
        require('./app.js');
}

cluster.on('exit', function(worker, code, signal) {
        var exitCode = worker.process.exitCode;

        console.log('worker' + worker.process.pid + ' died (' + exitCode + '). restarting...');

        if( typeof cluster.workers[worker.id] != "undefined" )
                cluster.workers[worker.id].delete();

        cluster.fork();
});

In my "app.js" file i defined REDIS for socket.io storing:

  io.set('store', new RedisStore({
            redisPub: pub,
            redisSub: sub,
            redisClient: client
          }));

So far, so good, all this works pretty nice.

When a client connects to the socket.io server, the cluster handles the connections with different workers.

My intention is, that a client can send a message to a specific another client, so the socket.io server have to find the socket from the receipient to send the message only to this user. The solution for me is, that i store all created socket ids for every user in an array and when sending a message, i select the relevant socket ids in the array, gets the sockets by id, and send the message to the socket(s).

This works very fine for a socket.io application, which is running only on one server. Now, i want to configure another server with the same programs, modules and packages. The load balancing will probably be handled by HAProxy. So the socket.io connections (sockets) will be stored and managed on Server A and Server B.

Example scenario:

User A connects to Server A and User B connects to Server B. That means, that User A has a socket on Server A und User B on Server B.

How is it possible, that the application knows, that it has to look for the socket of User B on Server B to send the message? On Server A it won't find the socket, because it was created on Server B.

Thx a lot!

4

2 回答 2

6

当您进行水平扩展时,您需要在服务器之间共享一个数据存储。方便的是,你已经有了一个理想的,那就是 Redis。您不需要将套接字映射保存在数组中,而是需要将其推送到 Redis 中,然后从那里进行查找。

然后,您可以决定让服务器相互发送消息,或者更可扩展地,也可以通过 Redis 发送消息。因此,每个服务器都会查看它在 Redis 上的队列,以查看它应该将哪些消息发送到哪些套接字。当服务器收到一条消息时,它会将其推送到 Redis 中,并发送给应该发送它的服务器。如果消息的顺序对您很重要,我建议将https://github.com/learnboost/kue视为 Redis 之上的层。

于 2013-07-28T10:44:11.947 回答
0

不要忘记在 NodeJS 前面包含 NGINX 并使用 PM2!

于 2019-11-20T03:37:02.830 回答