Krishnan Sriram
Krishnan Sriram

Reputation: 5519

Cannot connect from Express to remote Redis

I'm having issues with connecting to a remote Redis server. From my local client, when I connect from cli, everything looks fine

redis-cli -h 192.168.80.236 ping
PONG

If I attempt to connect from a simple Express application, I keep receiving the following error

Redis PUBLISHER NodeJS application in port 3000
events.js:187
      throw er; // Unhandled 'error' event
      ^

Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1128:14)
Emitted 'error' event on RedisClient instance at:
    at RedisClient.on_error (/Users/sriramk/Projects/Personal/Express/simpleredis/node_modules/redis/index.js:341:14)
    at Socket.<anonymous> (/Users/sriramk/Projects/Personal/Express/simpleredis/node_modules/redis/index.js:222:14)
    at Socket.emit (events.js:210:5)
    at emitErrorNT (internal/streams/destroy.js:92:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
    at processTicksAndRejections (internal/process/task_queues.js:80:21) {
  errno: 'ECONNREFUSED',
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 6379
}

What baffles me is, Redis connecting to "localhost", when it should really be connecting to remote server. Initially my thought was, may be some auth issues causing the connection problems. But if redis-cli can connect without an hitch, what's the problem with NodeJS Full code can be found in this location - https://github.com/KrishnanSriram/simpleredis

I'd appreciate you thoughts/advice. I'm totally stopped because of this issue

Upvotes: 2

Views: 2837

Answers (1)

eol
eol

Reputation: 24565

I've checked out your repo and in your server.js I can see that you try to instantiate a redis client twice.

Once with

const client = redis.createClient(); 

and then with

const publisher = redis.createClient(6379, '192.168.80.236');

Removing const client = redis.createClient(); should fix the issue as this would try to connect to localhost on the default port.

Upvotes: 3

Related Questions