3

Can anyone solve this problem? I'm a node.js and redis beginner.

The function createClient() can take 2 arguments: port and host. But I'm still stuck with this problem.

var client = redis.createClient(); 
client.on('connect', function(){
 console.log('Redis client connected');
});

Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379 C:\Users\my-pc\Desktop\FINAL>node index 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 (C:\Users\my-pc\Desktop\FINAL\node_modules\redis\index.js:406:14) at Socket. (C:\Users\my-pc\Desktop\FINAL\node_modules\redis\index.js:279: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 }

Dragonthoughts
2,2348 gold badges27 silver badges28 bronze badges
asked Nov 14, 2019 at 10:24
3
  • What is the actual problem? Commented Nov 14, 2019 at 10:28
  • Redis only config on port 6379. And even in my index.js file, the app listen on port 6379 still causes this problem. Commented Nov 14, 2019 at 11:18
  • is your redis service running? Commented Nov 14, 2019 at 12:14

6 Answers 6

6

This is a very common but simple issue whenever i had also started working with docker multiple local containers.

See, you have created a node app and also a redis server, but both are separated, you didn't build any communication between them, that's why you are facing this type of problems.

Now, first you have to build a connection between the node app and redis-server so that they can communicate between then.

For doing this, just simply follow the instructions, hopefully this will solve your issues.

Step-1: For building connection between multiple local containers, you have two options to do this,

* Use docker CLI's networking features.
* Or, simply use docker-compose to solve the issue, i prefer docker-compose and i will show how to do this using docker-compose.

Docker-compose: Compose is a tool for defining and running multi-container docker applications. Overview of docker-compose

Step-2: Make a docker-compose .yml file in your project directory, named by "docker-compose.yml"

Step-3: Write the following code in your "docker-compose.yml" file:

version: "2.0"
services: 
 redis-server: 
 image: "redis"
 node-app: 
 build: .
 ports: 
 - "8080:8080"

Step-4: Inside your redis.createClient() funstion, simply pass your host and port parameters in index.js file

const client = redis.createClient({
 host: "redis-server",
 port: 6379
});

Step-5: Build and run your docker container using docker-compose from docker-compose CLI

docker-compose up --build

Step-6: And finally, open your browser and hit the following URL:

localhost:8080
answered Sep 10, 2020 at 17:08
Sign up to request clarification or add additional context in comments.

1 Comment

OP didn't mention anything about Docker so it is less likely that issue is related to Docker.
6

just for your information (I wasted this evening with that)

This does not work (it defaults connecting to localhost:6379 whatever you put in the url):

const redisURL = "redis://foo.bar.org:6379"
redis.createClient( redisURL )

but this DOES WORK

redis.createClient( {url: redisURL} )

Hope I saved your time.

answered Nov 25, 2021 at 20:05

2 Comments

Ugh.. thanks for the tip. I just wasted to much time on this also. Sounds like bug.
Thank you so much for this. Wasted so many hours due to this issue :/
3

Based on the error code being 'ECONNREFUSED' are you positive that you're actually running a redis-server on localhost and port 6379 before you try to connect the client?

answered Nov 14, 2019 at 12:16

2 Comments

I forgot to turn on the sever.
Everything's OK now.
2

You simply need to add socket and it will magically work.

const client = redis.createClient({
 socket: {
 host: process.env.REDIS_HOST,
 port: process.env.REDIS_PORT,
 },
});

You are welcome!

Tyler2P
2,37030 gold badges26 silver badges34 bronze badges
answered Nov 6, 2023 at 11:02

Comments

0

You have to use socket object in it. also could refer to https://redis.io/docs/latest/operate/oss_and_stack/management/security/acl/

const client = createClient({
 username: 'default', // use your Redis user. More info 
 password: 'secret', // use your password here
 socket: {
 host: 'my-redis.cloud.redislabs.com',
 port: 6379,
 tls: true,
 key: readFileSync('./redis_user_private.key'),
 cert: readFileSync('./redis_user.crt'),
 ca: [readFileSync('./redis_ca.pem')]
 }
});
client.on('error', (err) => console.log('Redis Client Error', err));
await client.connect();
await client.set('foo', 'bar');
const value = await client.get('foo');
console.log(value) // returns 'bar'
await client.disconnect();

answered Dec 23, 2024 at 22:00

Comments

-1

I have faced a similar issue when i started with Redis on nodejs.

The problem is the redis server connection has not been established yet, but still you are using your connection. You can use async and await until your connection is established.

You can use the following code:

var client = redis.createClient();
client.connect().then(async () => {
 client.on('error', err => {
 console.log('Error ' + err);
 logger.info("Error is her");
 });
});

I hope your issue is solved. If not, You can revert to this thread

Tyler2P
2,37030 gold badges26 silver badges34 bronze badges
answered Mar 3, 2023 at 4:44

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.