Report this

What is the reason for this report?

Redis client connects to the redis remote server but can't read or write on it

Posted on October 14, 2015

I’ve to manage two servers ‘Node_server’ and a redis server ‘Redis_server’ by making the connection possible from a node/express application on the former for using the latter’s database.

For the purpose of local tests, i’ve also installed another redis server ‘LocalRedis_OnNodeServer’ locally on ‘Node_server’.

I have configured the ‘Redis_server’ to accept external connections as in https://www.digitalocean.com/community/tutorials/how-to-set-up-a-redis-server-as-a-session-handler-for-php-on-ubuntu-14-04 and when trying to connect from the 'LocalRedis_OnNodeServer’s redis-cli i was able to access the remote ‘Redis_server’ database and reading and writting operations were also possible through it.

I still didn’t configure any security’s mesures for ‘Redis_server’ (as to edit iptables…) and i’ve only set a password (requirepass in the redis.conf) and i still don’t know if a more secured authentification can be made with SSH on this server (or to configure my application code for it too).

The following is the app.js file which contains the express’s session and the RedisStore (receiving a redis client as args) passed to the session.

/*************************************** app.js ******************************************************/ var express = require(‘express’); var app = express(); var routes = require(‘./routes’); var errorHandlers = require(‘./middleware/errorhandlers’); var log = require(‘./middleware/log’); var partials = require(‘express-partials’); var cookieParser = require(‘cookie-parser’); var session = require(‘express-session’); var RedisStore = require(‘connect-redis’)(session); var bodyParser = require(‘body-parser’); var redis = require(‘redis’); var config = require(‘./config’);

app.set(‘view engine’, ‘ejs’); app.set(‘view options’, {defaultLayout: ‘layout’});

app.use(partials()); app.use(log.logger); app.use(express.static(__dirname + ‘/static’)); app.use(cookieParser(config.secret));

console.log(config.redisConf);

var redisClient = redis.createClient(config.redisConf);

redisClient.on(‘connect’, function() { console.log(‘connected to redis!!’); });

redisClient.set(‘framework’, ‘AngularJS’, function(err, reply) { console.log('the framwork var was SET to AngularJS : the following is the server answer : '); console.log(reply); });

app.use(session({ secret: config.secret, resave: false, saveUninitialized: true, store: new RedisStore({client: redisClient})
}));

// right after the session app.use(bodyParser.json()); app.use(bodyParser.urlencoded({extended: false}));

app.get(‘/’, routes.index); app.get(‘/login’, routes.login); app.post(‘/login’, routes.loginProcess); app.get(‘/chat’, routes.chat); app.get(‘/account/login’, routes.login);

app.get(‘/error’, function(req, res, next){ next(new Error(‘A contrived error’)); });

app.use(errorHandlers.error); app.use(errorHandlers.notFound);

app.listen(config.port); console.log("App server running on port " + config.port); /********************************************************************************************************/

and this is the config.js :

/*********************************** config.js ****************************************************/

var config = { port: 3000, secret: ‘secret’, redisConf: { host: ‘xxx.xxx.xxx.xxx.’, // The redis’s server ip port: ‘6379’, pass: ‘the_redis_pass’ } };

module.exports = config; /********************************************************************************************************/

I’ve tested the app connection to the local redis server ‘LocalRedis_OnNodeServer’ and and it was successful as well as reading and writing keys (for the set and get…). which is a proof that the app code is fine. But when i changed from the redis host (redisConf.host ) from the local 127.0.0.1 to the ‘Redis_server’ ip, only the connection occurs (the redisClient.on(‘connect’ callback logs that it’s ‘connected to redis!!’ but reading and writting functions(get and set) fail as their callbacks haven’t been triggered and the other problem was that the express’s session couldn’t be created and its value remains to undefined.

I wonder why the connection as well as reading and writting operations were all possible within the local redis server’s client on the 'Node_server’s shell (the redis-cli) to the remote server while the redisClient in application code failed.

Best regards.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

I solved it first by adding

client.on("error", function (err) {
    console.log("Error " + err);
});

so i could see what was wrong (indeed i made the mistake by thinking that the redisClient belongs to the connect-redis module so i didn’t see all these available options on the node_redis module…) and thanks to this answer and finally I saw that it was better to move the get methode inside the set’s callback…

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.