Socket IO econnrefused on port 3000 - javascript

I'm running a node server using socket.io 0.9.16 and after restarting
my server recently I got the above mentioned error:
{ [Error: connect ECONNREFUSED]
stack: 'Error: connect ECONNREFUSED\n at exports._errnoException (util.js:676:11)\n at Object.afterConnect [as oncomplete] (net.js:938:19)\n --------------------\n at Protocol._enqueue (
/var/www/node_modules/mysql-activerecord/node_modules/mysql/lib/protocol/Protocol.js:110:48)\n at Protocol.handshake (/var/www/node_modules/mysql-activerecord/node_modules/mysql/lib/protocol/Protoc
ol.js:42:41)\n at Connection.connect (/var/www/node_modules/mysql-activerecord/node_modules/mysql/lib/Connection.js:98:18)\n at Connection._implyConnect (/var/www/node_modules/mysql-activerecord
/node_modules/mysql/lib/Connection.js:296:10)\n at Connection.query (/var/www/node_modules/mysql-activerecord/node_modules/mysql/lib/Connection.js:154:8)\n at get (/var/www/node_modules/mysql-ac
tiverecord/index.js:387:15)\n at Manager.<anonymous> (/var/www/mutual.js:101:71)\n at Manager.authorize (/var/www/node_modules/socket.io/lib/manager.js:910:31)\n at Manager.handleHandshake (/
var/www/node_modules/socket.io/lib/manager.js:786:8)\n at Manager.handleRequest (/var/www/node_modules/socket.io/lib/manager.js:593:12)',
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
fatal: true }
nmap localhost returns:
Starting Nmap 5.21 ( http://nmap.org ) at 2014-07-22 21:02 EDT
Nmap scan report for localhost (127.0.0.1)
Host is up (0.0000050s latency).
Hostname localhost resolves to 2 IPs. Only scanned 127.0.0.1
rDNS record for 127.0.0.1: localhost.localdomain
Not shown: 992 closed ports
PORT STATE SERVICE
22/tcp open ssh
25/tcp open smtp
53/tcp open domain
80/tcp open http
587/tcp open submission
3000/tcp open ppp
3306/tcp open mysql
9000/tcp open cslistener
netstate -an | grep "LISTEN " returns
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:3000 0.0.0.0:* LISTEN
...
iptables --list returns
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- anywhere anywhere tcp dpt:3000
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Relevant server code:
var mysql = require('mysql-activerecord'),
marked = require('marked'),
conn = new mysql.Adapter({
server: 'server-name',
username: 'user',
password: 'pass',
database: 'database'
}),
moment = require('moment');
var io = require('socket.io').listen(3000);
Relevant client code:
a=io.connect("http://server-name:3000/",{query:stuff})
Thanks in advance for the help. Let me know if you need anymore information. Sorry
if this post is a bit long.

The ECONNREFUSED has nothing to do with socket.io, if you look at the backtrace provided in the error, you can see it's coming from the mysql module. So it appears that your mysql server is not listening on server-name on port 3306 (or at least something is blocking access to it).

Related

Error: connect ETIMEDOUT xxx.xx.xxx.xxx:587 Nodemailer script stopped working with outlook throws

A few months ago I created this script to help me send multiple mails at once instead of sending them one by one, and it was working perfectly till now.
For this script, I've used dotenv, nodemailer, and nodemon (only in development, when I finished it I started using npm run start to run it)
(function main() {
const { contacts } = contacts_list;
try {
const { MAIL_ACCOUNT, MAIL_PASSWORD } = process.env;
const transport = nodemailer.createTransport({
// Uses 'pool' attribute: The same connection is used to send up to 100 mails before being disposed.
pool: true,
// Sets the number of max connections per transport. Microsoft accepts up to 1 parallel connection for the same client.
maxConnections: 1,
logger: true,
debug: true,
service: "hotmail",
auth: {
user: MAIL_ACCOUNT,
pass: MAIL_PASSWORD
},
tls: { rejectUnauthorized: false }
})
// Change mail parameters such as subject, recipients, content, etc
const mailParams = {
from: `test <${MAIL_ACCOUNT}>`,
to: '',
attachments: [],
subject: `test`,
text: `test`,
html: `<h1>test</h1>`
}
// Mail every contact once at a time with its corresponding attachments
contacts.forEach(async (contact) => {
mailParams.to = contact.email;
mailParams.attachments = [...contact.attachments];
await transport.sendMail(mailParams);
})
} catch (err) {
console.error(err);
}
})();
I've already scanned for blocked ports, disabled firewall when trying to use it and antivirus as well. None of these approaches worked.
It throws the following error:
node:internal/process/promises:246
triggerUncaughtException(err, true /* fromPromise */);
^
Error: connect ETIMEDOUT xxx.xx.xxx.xxx:587
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1161:16) {
errno: -4039,
code: 'ESOCKET',
syscall: 'connect',
address: 'xxx.xx.xxx.xxx',
port: 587,
command: 'CONN'
}
Logger prints the following lines:
[2022-02-03 01:39:22] DEBUG Creating transport: nodemailer (6.7.2; +https://nodemailer.com/; SMTP (pool)/6.7.2[client:6.7.2])
[2022-02-03 01:39:22] DEBUG Sending mail using SMTP (pool)/6.7.2[client:6.7.2]
[2022-02-03 01:39:22] DEBUG Sending mail using SMTP (pool)/6.7.2[client:6.7.2]
[2022-02-03 01:39:22] INFO [#1] Created new pool resource #1
[2022-02-03 01:39:22] DEBUG [#1] Assigned message <567cc726-524b-0bda-5a52-698109ae7d78#hotmail.com> to #1 (1)
[2022-02-03 01:39:22] DEBUG [nHXJGQWMbA] Resolved smtp.live.com as xxx.xx.xxx.xxx [cache miss]
[2022-02-03 01:39:43] ERROR [nHXJGQWMbA] connect ETIMEDOUT xxx.xx.xxx.xxx:587
[2022-02-03 01:39:43] ERROR [#1] Pool Error for #1: connect ETIMEDOUT xxx.xx.xxx.xxx:587
[2022-02-03 01:39:43] ERROR Send Error: connect ETIMEDOUT xxx.xx.xxx.xxx:587
[2022-02-03 01:39:43] DEBUG [nHXJGQWMbA] Closing connection to the server using "destroy"
[2022-02-03 01:39:43] INFO [#1] Connection #1 was closed
If you know how to solve it, i'll be very grateful if you lend me a hand!
SOLUTION:
Finally, I found the solution thanks to #Marco Strahilov 's answer in other post. When instantiating the transport, set the service property to 'smtp-mail.outlook.com' instead of hotmail. I don't know for sure why 'hotmail' stopped working nor why 'stmp-email.outlook.com' works now.
go to
Settings => View All Outlook settings => Mail => Sync email
here is info for your correct settings IMAP, POP, SMTP
Finally, I found the solution thanks to #Marco Strahilov 's answer in other post.
When instantiating the transport, set the service property to 'smtp-mail.outlook.com' instead of hotmail. I don't know for sure why 'hotmail' stopped working.

error: password authentication failed for user "myuser"

If I have a record in /etc/postgresql/9.4/main/pg_hba.conf which specifically trusts my specific user
# TYPE DATABASE USER ADDRESS METHOD
local all myuser trust
Since I'm on debian I restart postgresql like this
sudo /etc/init.d/postgresql restart
Here is my entire source file testing this out:
const pg = require('pg');
const connectionString = "postgres://myuser:mypassword#localhost/mydbname";
const client = new pg.Client(connectionString);
client.connect();
const query = client.query('SELECT * FROM USERS');
query.on('end', () => { client.end(); });
and this is the error I consistently get:
error: password authentication failed for user "myuser"
at Connection.parseE (/home/myuser/webserver/node_modules/pg/lib/connection.js:539:11)
at Connection.parseMessage (/home/myuser/webserver/node_modules/pg/lib/connection.js:366:17)
at Socket.<anonymous> (/home/myuser/webserver/node_modules/pg/lib/connection.js:105:22)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at Socket.Readable.push (_stream_readable.js:134:10)
at TCP.onread (net.js:551:20)
It's also worth noting that doing the following works:
psql -h localhost -U myuser mydb
What am I doing wrong here?
As the documentation states, local is only for UNIX socket connections, while you are establishing a TCP connection to localhost.
Use a line like this:
host all myuser 127.0.0.1/32 trust
to trust all connections from localhost using IPv4 (use the adress ::1/128 for IPv6).

Error EADDRNOTAVAIL with Heroku nodejs server

I have developped a nodejs server on OpenShift and I am now trying to make the same kind of server on Heroku for a new project.
Here is a minimal code of my server:
var http = require('http');
var port = process.env.PORT || 8080;
var address = process.env.IP || '127.0.0.1';
console.log(address);
console.log(port);
var server = http.createServer(function(req, res)
{
res.writeHead(200, { 'Content-Type': 'text/html', 'Access-Control-Allow-Origin': '*' });
res.write(JSON.stringify({ valid: true }));
res.end();
});
server.listen(port, address);
Differences with my OpenShift server are:
The replacement of variables process.env.OPENSHIFT_NODEJS_PORT, process.env.OPENSHIFT_NODEJS_IP by process.env.PORT and process.env.IP.
I set the IP variable with this command line: heroku config:set IP=MYSERVERADDRESS.com
I cannot start my server, it always crash and I don't understand why, the logs of my server are:
MYSERVERADDRESS.com
41184
events.js:154
throw er; // Unhandled 'error' event
^
Error: listen EADDRNOTAVAIL MYSERVERIP:41184
at Object.exports._errnoException (util.js:893:11)
at exports._exceptionWithHostPort (util.js:916:20)
at Server.__dirname.Server.Server._listen2 (net.js:1233:19)
at net.js:1391:9
at GetAddrInfoReqWrap.asyncCallback [as callback] (dns.js:63:16)
at listen (net.js:1282:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:82:10)
My guess would be that your Heroku server will not have your (external) server address as its (internal) IP-number (the external IP-address will most likely terminate earlier in the Heroku network stack), which means that you cannot explicitly listen on it (which is basically what EADDRNOTAVAIL means).
Instead, don't use an address to listen to at all:
server.listen(port);

Node.js custom Module

When I run app.js, I get this error:
MBPdiDaniele3:Desktop danielemartini$ node app.js
events.js:141
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED 127.0.0.1:8080
at Object.exports._errnoException (util.js:870:11)
at exports._exceptionWithHostPort (util.js:893:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1063:14)
MBPdiDaniele3:Desktop danielemartini$
Here's the code for make_request.js:
var http = require('http');
var makeRequest = function(message) {
var options = {
host: 'localhost', port: 8080, path:'/', method: 'POST'
}
var request = http.request(options, function(response) {
response.on('data', function(data) {
console.log(data);
});
});
request.write(message);
request.end();
};
module.exports = makeRequest;
Here's the code for app.js:
var makeRequest = require('./make_request');
makeRequest("Here's looking at you, kid");
makeRequest("Hello, this is dog");
There are several possible causes :
No service is running on localhost:8080
A service runs on 8080 which refuses connections actively.
To check what is running, use one of those 2 commands (Unix) :
lsof -i :8080 -S
netstat -a | grep 8080
3 - Your running service isn't bound to your internal IP.
I've encountered the issue a few times on Cloud servers, where localhost/127.0.0.1 are not recognized. Try using the external IP of your machine (and make sure the firewall lets you make requests), or force your service to bind to all interfaces.
Hope it helps.

Why does my Node.js script not see any UDP packets that are being sent on a specific port?

I have the C# app that sends the UDP packets for the specific IP and port and it works well, because I have the other app that receives those packages. Now I would like to send them so that I can display it on my webpage - I've read that the node.js will fit perfectly here.
I installed the current version of node.js under windows environment and took the following code:
var PORT = 19777;
var MULTICAST_GROUP = "224.0.0.251";
var dgram = require("dgram");
var client = dgram.createSocket("udp4");
client.on("message", function(message, rinfo) {
console.log("received: ",message,rinfo);
});
client.on("listening", function() {
console.log("listening on ",client.address());
client.setBroadcast(true);
client.setTTL(64);
client.setMulticastTTL(64);
client.setMulticastLoopback(true);
client.addMembership(MULTICAST_GROUP);
client.send(payload, 0, payload.length, PORT, MULTICAST_GROUP, function(err,bytes) {
console.log("err: "+err+" bytes: "+bytes);
// client.close();
});
});
client.on("close", function() {
console.log("closed");
});
client.on("error", function(err) {
console.log("error: ",err);
});
client.bind(19777);
And now I started sending packets on the following ip "224.0.0.251" and port 19777 from my C# app - however, after runnint the client app I've got the following error:
C:\Users\user\Desktop>node client.js
error: { [Error: bind EADDRINUSE] code: 'EADDRINUSE', errno: 'EADDRINUSE', sysc
all: 'bind' }
What am I doing wrong? And - after fixing this issue - will I be able to just see all packets in the console? Thanks.
------------- EDIT:
Following the advice of John, I modified the app so that only node.js is listening now on that port. Thanks to this I made a progress and on the output of my console I get:
C:\Users\user\Desktop>node client.js
listening on { address: '0.0.0.0', family: 'IPv4', port: 19777 }
C:\Users\user\Desktop\client.js:19
client.send(payload, 0, payload.length, PORT, MULTICAST_GROUP, function(er
^
ReferenceError: payload is not defined
at Socket.<anonymous> (C:\Users\godyckim\Desktop\client.js:19:17)
at Socket.emit (events.js:104:17)
at startListening (dgram.js:139:10)
at dgram.js:230:7
at dns.js:85:18
at process._tickCallback (node.js:355:11)
at Function.Module.runMain (module.js:503:11)
at startup (node.js:129:16)
at node.js:814:3
but still no packets are visible.. Ok, I see that I didn't define the payload variable... What I want to achieve is to receive the packets that are sent by udp and either display it in the node.js console (that would be a start for me), or transfer it further so that I can see them in the browser. In the second case - what exactly I'm doing wrong now? Thanks!

Categories