Socket.io EADDRNOTAVAIL error - node.js

I oppened port of 120 on my firewall and i open ufw port on my server (Ubuntu 16.04)
But when run this code ;
var app = require('express')();
var http = require( "http" ).createServer( app );
var io = require( "socket.io" )( http );
http.listen(120, "xxxx.xxx.xx");
io.on('connection',function(socket){
console.log("A user is connected");
});
I get this error ;
throw er; // Unhandled 'error' event
^
Error: listen EADDRNOTAVAIL xxxx.xxxxxx:120
at Object.exports._errnoException (util.js:870:11)
at exports._exceptionWithHostPort (util.js:893:20)
at Server._listen2 (net.js:1224:19)
at listen (net.js:1273:10)
at net.js:1382:9
at nextTickCallbackWith3Args (node.js:452:9)
at process._tickCallback (node.js:358:17)
at Function.Module.runMain (module.js:444:11)
at startup (node.js:136:18)
at node.js:966:3

It may be that port 120 is already being used by something else.
You can use netstat to see what is listening on that port:
sudo netstat -plnt | grep ':120'
Another thing to mention is that low ports are sometimes reserved or blocked - you may want to just try a higher port, '1337' is always good for NodeJS :-)

Related

Error with NodeJS

I have this error when i try run my script.
{ [Error: listen EADDRINUSE :::8000]
code: 'EADDRINUSE',
errno: 'EADDRINUSE',
syscall: 'listen',
address: '::',
port: 8000 }
Error: listen EADDRINUSE :::8000
at Object.exports._errnoException (util.js:890:11)
at exports._exceptionWithHostPort (util.js:913:20)
at Server._listen2 (net.js:1234:14)
at listen (net.js:1270:10)
at Server.listen (net.js:1366:5)
at Server.listen.Server.attach (/usr/lib/node_modules/socket.io/lib/index.js:216:9)
at Timeout._onTimeout (/var/www/html/Bot/site.js:618:29)
at tryOnTimeout (timers.js:224:11)
at Timer.listOnTimeout (timers.js:198:5)
Script.js around ~605 Line
function load() {
query('SET NAMES utf8');
query('SELECT `id` FROM `rolls` ORDER BY `id` DESC LIMIT 1', function(err, row) {
if((err) || (!row.length)) {
logger.error('Cant get number from the last game');
logger.debug(err);
process.exit(0);
return;
}
currentRollid = row[0].id;
logger.trace('Roll '+currentRollid);
});
loadHistory();
setTimeout(function() { io.listen(8080); }, 3000);
}
618 line is
setTimeout(function() { io.listen(8080); }, 3000);
How i can fix this ? I try change io.listen Port but its still dont work.
Firstly, you should know that the EADDRINUSE error, gets thrown whenever the port that you're trying to listen on, is already in-use.
To fix this problem, you're going to need to free-up the port that your application is trying to listen on (if you're using Linux, perhaps you'll find this StackOverflow thread to be of use).
When creating servers in NodeJS, it's generally a good idea to listen for [process] exit events, so you can close your server, thus freeing up the port that it's listening on.
Here's some great documentation that provides information about different events that can be fired before a NodeJS process is terminated:
process_event_exit
process_event_uncaughtexception
process_signal_events
Going based on the snippets that you provided, I'm making the assumption that you're using SocketIO.
This StackOverflow answer gives a great example about how to close a SocketIO server.

Bitfinex's Websocket API "hello world" results in ECONNREFUSED error

Bitfinex's Websocket API has the following demo on how to init a connection:
//using the ws library
var WebSocket = require('ws');
var w = new WebSocket("wss://api2.bitfinex.com:3000/ws");
w.onmessage = function(msg) {
console.log(msg.data);
};
Running that example with node.js version v5.9.1 and ws version 1.0.1 results in the following error:
events.js:154
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED 178.249.189.15:3000
at Object.exports._errnoException (util.js:890:11)
at exports._exceptionWithHostPort (util.js:913:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1061:14)
What is the cause of that error?
The server is refusing connection...it might be the port (:3000) or the Host name.
Current host for Version 2 is api.bitfinex.com - full url I use is:
wss://api.bitfinex.com/ws/2 as of (7/11/2017)

Openshift + Meteor = listen EACCES?

I deployed Meteor.js on a Node 0.6 + Mongo 2.2 Openshift cartridge with a custom Node 0.8.24 installed in the data dir (sort of like this tutorial).
I do set the right ports before calling the app. My code in server.js looks like that:
// Setup env
process.env.ROOT_URL = "http://" + (process.env.OPENSHIFT_APP_DNS || "localhost");
process.env.MONGO_URL = (process.env.OPENSHIFT_MONGODB_DB_URL + process.env.OPENSHIFT_APP_NAME) || "mongodb://localhost:27017/";
process.env.PORT = process.env.OPENSHIFT_NODEJS_PORT || 8000;
process.env.IP = process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0';
// Show connection details on startup
console.log("MONGO_URL IS: " + process.env.MONGO_URL);
console.log("ROOT_URL IS: " + process.env.ROOT_URL);
console.log("PORT: " + process.env.PORT);
console.log("IP: " + process.env.IP);
require(require('path').join(__dirname, 'main.js'));
Then, when I rhc app restart myappname the app, I get:
> node server.js
MONGO_URL IS: mongodb://<login>:<pass>#127.5.x.x:27017/<myapp>
ROOT_URL IS: http://<myappname>-<mydomain>.rhcloud.com
PORT: 8080
IP: 127.5.x.x
events.js:71
throw arguments[1]; // Unhandled 'error' event
^
Error: listen EACCES
at errnoException (net.js:770:11)
at Server._listen2 (net.js:893:19)
at listen (net.js:932:10)
at Server.listen (net.js:1006:9)
at dns.js:72:18
at process.startup.processNextTick.process._tickCallback (node.js:245:9)
npm info <mydomain>#0.0.1 Failed to exec start script
npm ERR! weird error 1
npm ERR! not ok code 0
main.js is he regular entry point for my Meteor app.
The env settings look like the right ones. Why do I keep getting this EACCESS?

Node js 0.10.7: cluster support for udp dgram?

I'm trying to run following node js application as mentioned https://github.com/joyent/node/issues/2194
var util = require("util"),
dgram = require("dgram"),
cluster = require('cluster');
var udp = dgram.createSocket("udp4");
var port = 1190;
if (cluster.isMaster) {
for (i = 0; i < 2; i++) {
cluster.fork();
}
} else {
util.log("starting udp server on port " + port);
udp.on("error", function (error) {
util.log("failed to bind to UDP port - " + error)
});
udp.bind(port);
}
The app exits immediately with the following output:
23 May 23:22:13 - starting udp server on port 1190
23 May 23:22:13 - starting udp server on port 1190
events.js:72
throw er; // Unhandled 'error' event
^
Error: write ENOTSUP - cannot write to IPC channel.
at errnoException (child_process.js:980:11)
at ChildProcess.target.send (child_process.js:455:16)
at Worker.send (cluster.js:401:21)
at sendInternalMessage (cluster.js:394:10)
at handleResponse (cluster.js:177:5)
at respond (cluster.js:192:5)
at Object.messageHandler.queryServer (cluster.js:242:5)
at handleMessage (cluster.js:197:32)
at ChildProcess.EventEmitter.emit (events.js:117:20)
at handleMessage (child_process.js:318:10)
Does anyone know what is going on? When running this without cluster, everything is fine.
It seems that cluster does not support udp?
Some specs:
Window 7 x64
node js 0.10.7
It says in the link your provided that support for UDP clustering was added in v0.11.14. It is likely that you simply need to update your version of node.js

running Hook.io on a different port

I tried to run hook.io with a different port, which killed the autodiscover features of the clients. But when I try to create the clients with the same port, they get an error.
Sever:
var oHook = hookio.createHook( {
'name' :'dispatch-hook',
'hook-port': 9999,
'hook-host': 'localhost'
} );
oHook.start();
Client:
var oHook = hookio.createHook( {
name :'client-hook',
"hook-port":9999,
"hook-host":'localhost'
});
oHook.connect();
Error:
events.js:66
throw arguments[1]; // Unhandled 'error' event
^
Error: listen EADDRINUSE
at errnoException (net.js:781:11)
at Server._listen2._connectionKey (net.js:922:26)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
Why does the client want to start a server?
You shouldn't provide a port for the hook trying to connect to the server hook. The existence of hook-port in options makes that hook a server

Resources