Node.js + Socket.io + Redis app via PM2 with large

2020-03-27 08:04发布

问题:

I'm new to both node.js and socket.io, but I'm trying to build a simple service that listens to Redis notifications (fed by PHP app), and broadcasts them to any users currently logged in, connected to a socket.io room e.g. 'site_name:user:user_id'.

I have it working, but the memory footprint of the Node app quickly gets larger and larger, going from 100mb to 200+mb pretty quickly with about 100 users online and actively browsing, and I'm wondering if I have something set up wrong here.

PM2 is handling the node app, and nginx is set up as reverse proxy.

Server side:

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var redis = require('redis');
var redisClient = redis.createClient();
var allClients = [];

server.listen(8890);

io.sockets.on('connection', function (socket) {
    allClients.push(socket);

    socket.on('subscribe', function(data) {
        console.log('Joining room', data.room);
        socket.join(data.room);
        redisClient.subscribe(data.room);
    })

    socket.on('disconnect', function() {
        console.log('Disconnect');
        var i = allClients.indexOf(socket);
        delete allClients[i];
    });

});

// Watch for connection errors and log
redisClient.on('error', function (err) {
    console.log('Error event - ' + redisClient.host + ':' + redisClient.port + ' - ' + err);
});

redisClient.on('message', function(room, message) {
    console.log('New message: ' + message + '. In room: ' + room);
    io.sockets.in(room).emit('message', message);
});

Client side:

// connect to socket
socket = io.connect('http://localhost:8890');

// subscribe to user's room once connected
socket.on('connect', function(data){
    socket.emit('subscribe', { room: site_name + ':user:' + user_id });
});

// show messages from user's "room" via redis notifications
socket.on('message', function (response) {
    var json = new Hash(JSON.decode(response, true) || {});
    roar.alert(json.title, json.message, { link: json.link });
});

Seems like this should be a very lean app, no? What's a normal memory footprint for a simple node.js app?

The server starts up at 41mb, but even without anybody connecting to it, memory creeps up slowly, about 1mb a minute. Once I start connecting users, it bloats up quickly to 200+mb until I kill it.

I'm not clear on how best to handle the redisClient and socket connections as users connect & disconnect, and I thought that might be the issue. But seeing it creep up while idle is disconcerting.

  • PM2 v0.15.7
  • node v0.12.7
  • socket.io v1.3.7
  • express v4.13.3
  • nginx v1.6.2

Any help much appreciated.

回答1:

I have a similar setup although it is not released and have done no stress testing yet... but here is an idea for you:

Use the redis module for socketio (whether it is any better then the redisClient would be interesting to know). It uses a different client for pub'ing and sub'ing. The subClient uses detect_buffers.

var redisModule = require('socket.io-redis');
var redisAdapter= redisModule({
      host: redisClient.options.host
    , port: redisClient.options.port
    , pubClient: redisClient
    //, subClient: ... separate client that uses detect_buffers
});
io.adapter(redisAdapter);

then subscribe/disconnect looks like this:

socket.on('subscribe', function(room) {
    socket.join(room);
});
socket.on('disconnect', function() {
    console.log('user disconnected');
});

I've also read multiple times that at one point socketio was not the best and to instead use sockjs. No idea if that is still the case.

And... since I just realized it's been more than 2 months. Did you find anything to reduce your memory footprint?