Sharing JSON object between different clusters

Asked

Viewed 152 times

5

Good afternoon, I would like to know how to share a JSON object between different clusters. I currently have 3 clusters listening to three different doors. And I need to have an object saved in memory so that the three clusters can access the information of this object.

var express = require('express');
var net = require('net');
var Files = require('./lib/Files');

var app = express();
var port = 9000;

if(!myObject) {
    // Cria o objeto na memória para que os outros sockets possam acessar
}

// Cria o Servidor
net.createServer(function (socket) {
    if (config.STRLOG) {
        console.log("Dispositivo Conectado: " + socket.remoteAddress + ":" + socket.remotePort);
    }

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

    socket.on('error', function (err) {
        console.log('> SOCKET ERROR');
        Files.SocketErrorLog(port, err);
        console.log('------------------');
    });
}).listen(port);

module.exports = app;
  • Is this file static? or should it be accessed and/or modified by different servers? Is there a database?

  • The idea is that when instantiated the object, the information is searched in the bank. After instantiated it is put into memory so that the other sockets can access it.

1 answer

2


You can use the REDIS database to save your json and your clusters can access the objects there ex:

var express = require('express'),
redis = require('redis'),
cookieParser = require('cookie-parser'),
session = require('express-session'),
redisStore = require('connect-redis')(session),
logger = require('morgan'),
bodyParser = require('body-parser');


var client = redis.createClient(),//CREATE REDIS CLIENT
    app = express();

app.use(cookieParser('strongsecretcode'));
app.use(session(
    {
        secret: 'strongsecretcodehere', 
        store: new redisStore({ host: 'localhost', port: 6379, client: client }),
        saveUninitialized: false, // don't create session until something stored,
        resave: false // don't save session if unmodified
    }
));

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

var router = express.Router();

router.get('/yoursession/set/:value', function(req, res) {
    req.session.user = req.params.value;
    res.send('session saved in Redis');
});

app.get('/yoursession/get/', function(req, res) {
    if(req.session.user)
        res.send(req.session.user);
    else
        res.send("no session value stored in Redis ");
});

app.use('/', router);
var server = app.listen(2001, function() {
    console.log('REDIS SESSION server is listening on port %d', server.address().port);
});

In the book: Real-time web applications with Node.js Page 29 explains how to use redis in cluster-based applications.

  • Your answer is welcome but it is important to translate the commented parts, indicate the source from which you copied this code and give an explanation of the code with your content. So the answer would be much better :) Tell me when you have done that I give +1 happily!

  • Interesting! The speed of access to this database is the same for direct object access in memory?

  • 1

    "Redis stores and searches, in its database, key-value elements, extremely fast, because it keeps the data in memory for a large part of the time, making in short periods the synchronization of the data with hard disk. It is considered a key-value Nosql" Excerpt from: Caio Ribeiro Pereira. "Real-time web applications with Node.js."

Browser other questions tagged

You are not signed in. Login or sign up in order to post.