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 :-)
I have a nodejs server using socketio for real-time comms.
var app = require('http').createServer(handler)
, io = require('socket.io')().listen(app)
function init() {
app.listen(process.env.PORT || 8888);
};
io.sockets.on('connection', function (socket) {
socket.setNoDelay(true);
onSocketConnection(socket);
});
Issue is, every time I call socket.setNoDelay(true); it's kicking back:
E:[path]\server.js:58
socket.setNoDelay(true);
^
TypeError: undefined is not a function
at Namespace.<anonymous> (E:\[path]\server.js:58:12)
at Namespace.emit (events.js:107:17)
at Namespace.emit (E:\path]\node_modules\socket.io\lib\namespace.js:205:10)
at E:\[path]\node_modules\socket.io\lib\namespace.js:172:14
at process._tickCallback (node.js:355:11)
I can't seem to find any documentation as to why this is happening. Anyone else see this?
Specs:
> Windows Environment
> node version: 0.12.4
> socket.io version: 1.3.6
Because socket isn't a net.Socket, it's a socket.io Socket. You'll notice that there is no setNoDelay method on a socket.io Socket.
The socket.io websocket sever automatically disables Nagle on the underlying TCP socket.
I'm using node.js with socket.io to give my web page access to character data served by a TCP socket. I'm quite new to node.js.
User ----> Web Page <--(socket.io)--> node.js <--(TCP)--> TCP Server
The code is mercifully brief:
io.on('connection', function (webSocket) {
tcpConnection = net.connect(5558, 'localhost', function() {});
tcpConnection.on('error', function(error) {
webSocket.emit('error', error);
tcpConnection.close();
});
tcpConnection.on('data', function(tcpData) {
webSocket.emit('data', { data: String.fromCharCode.apply(null, new Uint8Array(tcpData))});
});
});
It all works just fine in the normal case, but I can't guarantee that the TCP server will be there all the time. When it isn't, the TCP stack returns ECONNREFUSED to node.js - this is entirely expected and I need to handle it gracefully. Currently, I see:
events.js:72
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED
at errnoException (net.js:904:11)
at Object.afterConnect [as oncomplete] (net.js:895:19)
... and the whole process ends.
I've done a lot of searching for solutions to this; most hits seem to be from programmers asking why ECONNREFUSED is received in the first place - and the advice is simply to make sure that the TCP server is available. No discussing of handling failure cases.
This post - Node.js connectListener still called on socket error - suggests adding a handler for the 'error' event as I've done in the code above. This is exactly how I would like it to work ... except it doesn't (for me), my program does not trap ECONNREFUSED.
I've tried to RTFM, and the node.js docs at http://nodejs.org/api/net.html#net_event_error_1 suggest that there is indeed an 'error' event - but give little clue how to use it.
Answers to other similar SO posts (such as Node.js Error: connect ECONNREFUSED ) advise a global uncaught exception handler, but this seems like a poor solution to me. This is not my program throwing an exception due to bad code, it's working fine - it's supposed to be handling external failures as it's designed to.
So
Am I approaching this in the right way? (happy to admit this is a newbie error)
Is it possible to do what I want to do, and if so, how?
Oh, and:
$ node -v
v0.10.31
I ran the following code:
var net = require('net');
var client = net.connect(5558, 'localhost', function() {
console.log("bla");
});
client.on('error', function(ex) {
console.log("handled error");
console.log(ex);
});
As I do not have 5558 open, the output was:
$ node test.js
handled error
{ [Error: connect ECONNREFUSED]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect' }
This proves that the error gets handled just fine... suggesting that the error is happening else-where.
As discussed in another answer, the problem is actually this line:
webSocket.emit('error', error);
The 'error' event is special and needs to be handled somewhere (if it isn't, the process ends).
Simply renaming the event to 'problem' or 'warning' results in the whole error object being transmitted back through the socket.io socket up to the web page:
webSocket.emit('warning', error);
The only way I found to fix this is wrapping the net stuff in a domain:
const domain = require('domain');
const net = require('net');
const d = domain.create();
d.on('error', (domainErr) => {
console.log(domainErr.message);
});
d.run(() => {
const client = net.createConnection(options, () => {
client.on('error', (err) => {
throw err;
});
client.write(...);
client.on('data', (data) => {
...
});
});
});
The domain error captures error conditions which arise before the net client has been created, such as an invalid host.
See also: https://nodejs.org/api/domain.html
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?
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