How do I use redis in node.js? - node.js

I am trying to use redis using this segment of code:
const redis = require('redis');
const client = redis.createClient();
I am getting the following error:
events.js:174
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:1097:14)

You will need to first install redis on your system. To do so, follow the instructions here.
Then run the following command in your command prompt/terminal:
redis-server
If you want to run redis in the background without having to start it everytime, then you can use:
redis-server --daemonize yes
You can check if your server is running by using the command:
redis-cli set a 3 & redis-cli get a
If this works then your redis server has started successfully.
After this, your previous code should be able to connect to the redis-server provided the server is running on the default port (6379).

Related

ONLYOFFICE Developer Edition demo issue - error downloadFile

I installed and run OnlyOffice docker image on my local windows machine with this command:
docker run -i -t -d -p 9950:80 --restart=always -v C:/work/only_office/logs:/var/log/onlyoffice onlyoffice/documentserver-de
Then run commands described on this page - http://localhost:9950/welcome/ for testing OnlyOffilce:
docker exec 2a9aa47c18a2 sudo supervisorctl start ds:example
docker exec 2a9aa47c18a2 sudo sed "s,autostart=false,autostart=true," -i /etc/supervisor/conf.d/ds-example.conf
This page - http://localhost:9950/example/ starts working. But when I try to create, open or upload document I am getting this error:
And errors in the log files:
[2020-07-07T12:30:24.892] [ERROR] nodeJS - error downloadFile:url=http://127.0.0.1:9950/example/files/172.17.0.1/new%20(2).docx;attempt=1;code:ECONNREFUSED;connect:null;(id=172.17.0.1http___127.0.0.1_9950_example_files_172.17.0.1_new_20_2_.docx1594125018845)
Error: connect ECONNREFUSED 127.0.0.1:9950
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1107:14)
[2020-07-07T12:30:25.894] [ERROR] nodeJS - error downloadFile:url=http://127.0.0.1:9950/example/files/172.17.0.1/new%20(2).docx;attempt=2;code:ECONNREFUSED;connect:null;(id=172.17.0.1http___127.0.0.1_9950_example_files_172.17.0.1_new_20_2_.docx1594125018845)
Error: connect ECONNREFUSED 127.0.0.1:9950
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1107:14)
[2020-07-07T12:30:26.897] [ERROR] nodeJS - error downloadFile:url=http://127.0.0.1:9950/example/files/172.17.0.1/new%20(2).docx;attempt=3;code:ECONNREFUSED;connect:null;(id=172.17.0.1http___127.0.0.1_9950_example_files_172.17.0.1_new_20_2_.docx1594125018845)
Error: connect ECONNREFUSED 127.0.0.1:9950
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1107:14)
[2020-07-07T12:30:24.881] [ERROR] nodeJS - postData error: docId = 172.17.0.1http___127.0.0.1_9950_example_files_172.17.0.1_new_20_2_.docx1594125018845;url = http://127.0.0.1:9950/example/track?filename=new%20(2).docx&useraddress=172.17.0.1;data = {"key":"172.17.0.1http___127.0.0.1_9950_example_files_172.17.0.1_new_20_2_.docx1594125018845","status":1,"users":["uid-1"],"actions":[{"type":1,"userid":"uid-1"}]}
Error: connect ECONNREFUSED 127.0.0.1:9950
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1107:14)
I tried to switch off firewall and test it on another Windows PC but the issue is the same. Any ideas how to fix it?
Check this one out. Seems like some process is running on the port you are using. Try switching your ports.
References:
"The ECONNREFUSED Connection refused by the server error, is a common error returned by the Filezilla FTP client. This error indicates that the user is trying to connect to your server and is unable to connect to the port."
https://community.progress.com/s/article/Connection-Error-ECONNREFUSED-Connection-refused-by-the-server
ECONNREFUSED on running localhost server from NodeJS
Node.js Error: connect ECONNREFUSED
I had the same problem and after trying for a while I just try following the documentation and it worked, you have to run the docker image in port 80, that will solve the error.
docker run -i -t -d -p 80:80 --restart=always -v C:/work/only_office/logs:/var/log/onlyoffice onlyoffice/documentserver-de
I'm searching for change the port in a proper way. Let me know if you find out.
According to support's answer the current last version (5.5.3) of OnlyOffice is not working on Windows docker:
Unfortunately, at the moment we do not recommend using this method of
installing Document Server. If you would like to use a docker
installation, please use this Document Server installation guide on
*NIX systems.
I run docker image on CentOs server. Now everything is working as expected.

Openshift redis cartridge with strange exceptions ETIMEDOUT on socket.io script

I have a socket.io node script running that acts as a chat server. I had it running on a local server ok, and have since tried moving it over to openshift.
I used the following cartridge to get it working - https://github.com/smarterclayton/openshift-redis-cart/issues
While running the local version of the server, while connecting to the openshift redis, I get strange timeouts exactly 127 seconds after I run the script.
events.js:72
throw er; // Unhandled 'error' event
^
Error: Redis connection to <server> failed - connect ETIMEDOUT
at RedisClient.on_error (/path/node_modules/redis/index.js:196:24)
at Socket.<anonymous> (/path/node_modules/redis/index.js:106:14)
at Socket.emit (events.js:95:17)
at net.js:441:14
at process._tickCallback (node.js:442:13)
I have seen the events.js:72 one before, but its usually when I try to run a node script with something already running on that port.
Pretty sure the script itself is fine, as when I swap out the redis server for my local one, it will sit there for days and work fine.
So my question: why is my socket.io script killing itself after exactly 127 seconds when redis is hosted on openshift?
Are you using the rhc port-forward command so that you can connect to redis on OpenShift? Otherwise that port is not publicly available.

Node Redis EC2 errconnrefuced

There's some strange behavior i've found with node_redis on Aws EC2.
Let's say i have two EC2 instances.
First one is used for DB (Redis, MYSQL, etc.) and it has an IP like so ip-1-1-1-1.eu-west-1.compute.internal
Second one is for app based on NodeJS and it's IP is ip-2-2-2-2.eu-west-1.compute.internal
In the Security Groups i've added this:
6379 sg-351a0441 (default)
6379 sg-333b9044 (quicklaunch-0)
I use node_redis module to communicate with Redis. I need to connect from one instance to another.
So now i (probably) can connect to Redis (first server) from another one:
redis = require('redis');
client = redis.createClient(6379, 'ip-1-1-1-1.eu-west-1.compute.internal');
But this way causes error:
Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED
It tries to connect (!) to 127.0.0.1 istead of 1.1.1.1.
I've said "OK. Let's try another way!"
redis = require('redis');
client = redis.createClient(6379, '1.1.1.1');
And.. BANG! The same error here:
Error: Redis connection to 1.1.1.1:6379 failed - connect ECONNREFUSED
So, maybe full redis url will help?
redis = require('redis');
client = redis.createClient(6379, 'redis://1.1.1.1');
No :(
Error: Redis connection to redis://10.248.118.246:6379 failed - getaddrinfo EADDRINFO
And same one when using internal url:
Error: Redis connection to redis://ip-1.1.1.1.eu-west-1.compute.internal:6379 failed - getaddrinfo EADDRINFO
So, what's wrong with it? Thanks in advance.

Error: Connection refused issue when trying to connect to a mqtt enabled node server hosted on heroku

So I basically wanted to host a mqtt pub/sub-enabled node server on Heroku. These are 2 guides I refer to:
https://github.com/adamvr/MQTT.js and https://gist.github.com/1826931
The mqtt pub/sub worked well when tested on localhost, deployment on heroku was fine, but when I tried testing on Heroku it didn't work
I tried 2 commands to subscribe, both showed the same errors.
Command 1:
mosquitto_sub -p <port> -h <host> -t <topic>
Error 1:
Error: Connection refused
Unable to connect (13).
Command 2:
mqtt_sub <port> <host> <topic>
Error 2:
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: connect ECONNREFUSED
at errnoException (net.js:670:11)
at Object.afterConnect [as oncomplete] (net.js:661:19)
My main.js (server) code is similar to this one - https://gist.github.com/1826931. The error seems more like a node problem than a mqtt pub/sub problem, and I am new to node, so not quite sure how to debug it. I put in some console.log lines but as expected it didn't print since it's not connected. There is a dyno running node main.js on Heroku fine.
Process State Command
------- ---------- ------------
web.1 up for 35m node main.js
Appreciate any advice on this issue.

Problems with RailwayJS (node.js): Redis connection to localhost:6379 failed - connect ECONNREFUSED

I found this web framework for node.js similar to Rails: http://railwayjs.com/
I followed all the steps:
sudo npm install railway -g
railway init blog && cd blog
npm install -l
railway generate crud post title content
# ensure mongodb is running
railway server 8888
open http://127.0.0.1:8888/posts
But I'm getting this error when I start the server:
alex#alex-System-Product-Name:~/blog$ sudo status mongodb
mongodb start/running, process 5836
alex#alex-System-Product-Name:~/blog$ railway server 8888
The "sys" module is now called "util". It should have a similar interface.
Railway server listening on port 8888 within development environment
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Redis connection to localhost:6379 failed - connect ECONNREFUSED
at RedisClient.on_error (/home/alex/blog/node_modules/railway/node_modules/jugglingdb/node_modules/redis/index.js:136:24)
at Socket.<anonymous> (/home/alex/blog/node_modules/railway/node_modules/jugglingdb/node_modules/redis/index.js:70:14)
at Socket.emit (events.js:67:17)
at Array.0 (net.js:299:25)
at EventEmitter._tickCallback (node.js:192:40)
Does anyone knows what's the problem?
I had to install Redis for Ubuntu (the OS I was using).
Then run redis-server in the terminal.

Resources