Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nodejs clustering application cause of connection not stable #196

Open
pishguy opened this issue Aug 5, 2016 · 0 comments
Open

Nodejs clustering application cause of connection not stable #196

pishguy opened this issue Aug 5, 2016 · 0 comments

Comments

@pishguy
Copy link

pishguy commented Aug 5, 2016

I'm implementing my nodejs server application with cluster, after successful this implementation my connection on android is not stable and reconnect every call sockets such as login, before implementation i don't have any problem and work fine

var cluster = require('cluster'), _portSocket = 3000, _portRedis = 6379, _HostRedis = 'localhost';

if (cluster.isMaster) {
    var server                                                = require('http').createServer(),
        socketIO = require('socket.io').listen(server), redis = require('socket.io-redis');

    socketIO.adapter(redis({host: _HostRedis, port: _portRedis}));
    for (var i = 0; i < 4; i++) {
        cluster.fork();
    }

    cluster.on('exit', function (worker, code, signal) {
        if (!worker.suicide) {
            cluster.fork();
        }
    });
}

if (cluster.isWorker) {

    var http = require('http');

    http.globalAgent.maxSockets = Infinity;

    var socket      = require('socket.io')(3000),
        express     = require('express'),
        app         = express(),
        server      = require('http').createServer(app),
        io          = socket.listen(server),
        mysql       = require('mysql'),
        multer      = require('multer'),
        uuid        = require('node-uuid'),
        datetime    = require('node-datetime'),
        moment      = require('moment'),
        bcrypt      = require('bcrypt'),
        request     = require('request'),
        redis       = require("redis-node"),
        redisIo     = require('socket.io-redis'),
        email       = require("emailjs"),
        redisClient = redis.createClient(),

    var connection =
            mysql.createConnection(
                {
                    host              : 'localhost',
                    user              : 'root',
                    password          : 'a',
                    database          : 'shoot',
                    multipleStatements: true
                });

    socket.adapter(redisIo({host: _HostRedis, port: _portRedis}));

    socket.sockets.on('connection', function (socket, pseudo) {

        socket.on('login', function (data) {
            console.log(data.username);
            login(data.username, data.password, function (success, value) {
               if (success)
                   redisClient.set(data.username, socket.id);
               socket.emit('login', {result: success, id: value});
            });
        });

        socket.on('userConnected', function (data) {
            ...
        });

    });
}


function formatDate(date) {
    ...
}

function login(username, password, callback) {
    ...
}

after call login socket i dont see any log and doesnt work and cause of reconnect

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant