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.