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

October 14, 2015 5k views
Redis Node.js

I've to manage two servers 'Nodeserver' and a redis server 'Redisserver' 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 'LocalRedisOnNodeServer' locally on 'Nodeserver'.

I have configured the 'Redisserver' to accept external connections as in and when trying to connect from the 'LocalRedisOnNodeServer'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(express.static(__dirname + '/static'));


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 : ');

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


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

app.get('/', routes.index);
app.get('/login', routes.login);'/login', routes.loginProcess);
app.get('/account/login', routes.login);

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


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: '', // The redis's server ip
port: '6379',
pass: 'theredispass'

module.exports = config;

I've tested the app connection to the local redis server 'LocalRedisOnNodeServer' 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 ( ) from the local 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.

1 comment
1 Answer

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..

Have another answer? Share your knowledge.