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 }
-
What is the actual problem?Dragonthoughts– Dragonthoughts2019年11月14日 10:28:02 +00:00Commented 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.Nguyen DucAnh– Nguyen DucAnh2019年11月14日 11:18:45 +00:00Commented Nov 14, 2019 at 11:18
-
is your redis service running?Babak Abadkheir– Babak Abadkheir2019年11月14日 12:14:59 +00:00Commented Nov 14, 2019 at 12:14
6 Answers 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
1 Comment
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.
2 Comments
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?
2 Comments
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!
Comments
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();
Comments
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