Meteor EADDRINUSE exception on remote method with Fibers - node.js

Below is my code in server/server.js file. When I call Meteor.apply('testMethod') I get Error: listen EADDRINUSE. I am running meteor app with meteorite, the only non-generic package installed is npm
var Fiber = Meteor.require('fibers');
var fiber = Fiber.current;
Meteor.methods({
testMethod: function(){
setTimeout(function(){fiber.run('test')}, 2000);
res = Fiber.yield();
console.log(res);
return res;
}
})
I know the most obvious solution here is to use Meteor's wrappers around fiber, but what I really want to implement is yielding from fiber on async call and then resuming within some event handler. And I have not found anything suitable for that.
Stack trace:
Error: listen EADDRINUSE
at errnoException (net.js:901:11)
at Server._listen2 (net.js:1039:14)
at listen (net.js:1061:10)
at net.js:1135:9
at dns.js:72:18
at process._tickCallback (node.js:415:13)

I answered this on the Meteor IRC.
Move this line:
var fiber = Fiber.current;
into the first line of the 'testMethod' function. Like so:
var Fiber = Meteor.require('fibers');
Meteor.methods({
testMethod: function(){
var fiber = Fiber.current;
setTimeout(function(){fiber.run('test')}, 2000);
res = Fiber.yield();
console.log(res);
return res;
}
});
I believe why you're experiencing this is because each user gets their own "fiber". So when the client calls the server, the current fiber is different than the fiber you declared in the outer scope.

Related

Why is socket.setNoDelay() throwing an error?

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.

The strange Node.js error after starting script

I use Node.js.
My server js script I run such:
node chat_server.js
After I get errors messages in terminal CentOS:
Express server listening on port undefined in development mode.
+ User undefined connected node_redis: no callback to send error: ERR wrong number of arguments for 'sadd' command
/home/who/public_html/node/node_modules/redis/index.js:582
throw err;
^ Error: ERR wrong number of arguments for 'sadd' command
at ReplyParser. (/home/who/public_html/node/node_modules/redis/index.js:317:31)
at ReplyParser.emit (events.js:95:17)
at ReplyParser.send_error (/home/who/public_html/node/node_modules/redis/lib/parser/javascript.js:296:10)
at ReplyParser.execute (/home/who/public_html/node/node_modules/redis/lib/parser/javascript.js:181:22)
at RedisClient.on_data (/home/who/public_html/node/node_modules/redis/index.js:547:27)
at Socket. (/home/who/public_html/node/node_modules/redis/index.js:102:14)
at Socket.emit (events.js:95:17)
at Socket. (_stream_readable.js:748:14)
at Socket.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:410:10)
Excuse me, but I do not understand the reason of these errors.
On what I should get attention and how fix it?
For example, I use command redis SADD: redis_cli.sadd( "user.friend:" + currentIdUser, data.idUser);
I have done a experiment, created a new text script:
var redis = require("redis");
var client = redis.createClient();
client.on("error", function (err) {
console.log("Error " + err);
});
client.sadd("users","naveen",function(err,reply){
console.log('Ok');
if(err)
throw err;
return reply;
});
It have given me in console: OK. It mean, that all works fine.
I think in my code:
redis_cli.sadd("userslist", currentIdUser);
the variable currentUser is simply empty or undefined. It gives me the next errors.
Problem was at my script in line:
redis_cli.sadd("userslist", currentIdUser);
The variable is undefined. It calls error redis.
Thank you all for help.

node.js - handling TCP socket error ECONNREFUSED

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

now.js : "Object has no method" error message when trying to start server.js

I installed node.js and now.js successfully.
For now.js, this is how I did:
npm install now -g
npm install now (had to add this one. Without it, I get a "Cannot find now..." error message)
When I start the node server and provide a server.js file like this:
var httpServer = require('http');
httpServer.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('Node is ok');
res.end();
}).listen(8080);
console.log('Server runs on http://xxxxx:8080/');
Everything is fine.
Now, I'm trying to add to this file a basic use of now.js:
var nowjs = require("now");
var everyone = nowjs.initialize(httpServer);
everyone.now.logStuff = function(msg){
console.log(msg);
}
I create an index.html file in the same folder (for testing purposes)
<script type="text/javascript" src="nowjs/now.js"></script>
<script type="text/javascript">
now.ready(function(){
now.logStuff("Now is ok");
});
</script>
This time, this is what I get on the terminal when starting the server:
Server runs on http://xxxxx:8080/
[TypeError: Object #<Object> has no method 'listeners']
TypeError: Object #<Object> has no method 'listeners'
at Object.wrapServer (/home/xxxx/node_modules/now/lib/fileServer.js:23:29)
at [object Object].initialize (/home/xxxx/node_modules/now/lib/now.js:181:14)
at Object.<anonymous> (/home/xxxx/server.js:10:22)
at Module._compile (module.js:444:26)
at Object..js (module.js:462:10)
at Module.load (module.js:351:32)
at Function._load (module.js:309:12)
at module.js:482:10
at EventEmitter._tickCallback (node.js:245:11)
Please keep in mind that I'm an absolute beginner.
Thank you for your help
'npm install -g' installs modules at a global level, often with the intent of providing system-wide binaries for terminal usage. Think Ruby Gems. If you want to include a module as part of your project you need to remove the -g.
Also, your httpServer variable is not your server but rather the http module. createServer() returns a server object which you want to capture with a variable to use in your nowjs.initialize() method as follows:
var http = require('http')
, now = require('now')
// Returns an Http Server which can now be referenced as 'app' from now on
var app = http.createServer(
//... blah blah blah
)
// listen() doesn't return a server object so don't pass this method call
// as the parameter to the initialize method below
app.listen(8080, function () {
console.log('Server listening on port %d', app.address().port)
})
// Initialize NowJS with the Http Server object as intended
var everyone = nowjs.initialize(app)

Node.js, dgram.setBroadcast(flag) fails due to "EBADF"

I'm using Node.js 0.6.9, and am trying to send a datagram broadcast package. Code:
var sys = require('util');
var net = require('net');
var dgram = require('dgram');
var message = new Buffer('message');
var client = dgram.createSocket("udp4");
client.setBroadcast(true);
client.send(message, 0, message.length, 8282, "192.168.1.255", function(err, bytes) {
client.close();
});
Running the code:
$ node test.js
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: setBroadcast EBADF
at errnoException (dgram.js:352:11)
at Socket.setBroadcast (dgram.js:227:11)
at Object.<anonymous> (/home/letharion/tmp/collision/hello.js:25:8)
at Module._compile (module.js:444:26)
at Object..js (module.js:462:10)
at Module.load (module.js:351:32)
at Function._load (module.js:310:12)
at Array.0 (module.js:482:10)
at EventEmitter._tickCallback (node.js:192:41)
Some googling reveals that "EBADF" means "The socket argument is not a valid file descriptor". But I don't understand enough about the problem for that to be helpful.
First of all, you seem to have trouble understanding the format of the stacktrace, so let's clarify it before we go to the actual error that is thrown here.
Format of a node.js Stacktrace
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
This part is just the location where the internal NodeJS logic choked up and put out the error below:
The actual error stacktrace follows, it shows the deepest location in the callstack first, so going down in the stack trace, brings you up in the call hierachy, eventually leading you to the point in your code where everything began.
Error: setBroadcast EBADF
at errnoException (dgram.js:352:11)
at Socket.setBroadcast (dgram.js:227:11)
at Object.<anonymous> (/home/letharion/tmp/collision/hello.js:25:8)
at Module._compile (module.js:444:26)
at Object..js (module.js:462:10)
at Module.load (module.js:351:32)
at Function._load (module.js:310:12)
at Array.0 (module.js:482:10)
at EventEmitter._tickCallback (node.js:192:41)
First it fails in dgram.js on line 352, dgram.js is a internal node.js module abstracting the "low level" code. Line 352 is in a function containing generic logic for throwing errors.
It was called at dgram.js in line 227, after a failed if check which wraps the call to the wrapped native UDP sockets setBroadcast method.
Going up one more layer, we end up at your hello.js file on line 25 with the client.setBroadcast(true); call.
The rest is more node.js code resulting from the initial load of the hello.js file.
The actual Error
The error thrown by the native code which node.js wraps here is EBADF looking this up in conjunction with UDP gives us:
EBADF
The socket argument is not a valid file descriptor.
By going further down into the node.js rabbit hole, we end up in the udp wrapper, which wraps the uv wrapper for the actual C implementation, in the uv wrapper we find:
/*
* Set broadcast on or off
*
* Arguments:
* handle UDP handle. Should have been initialized with
* `uv_udp_init`.
* on 1 for on, 0 for off
*
* Returns:
* 0 on success, -1 on error.
*/
Leading us to the conclusion that your socket has not been initialized yet.
In the end, binding the socket via client.bind(8000) fixed the missing initialization and made the program run.
The method setBroadcast should be called on 'listening' event or passed as callback in bind method:
var socket = dgram.createSocket('udp4');
socket.on('listening', function(){
socket.setBroadcast(true);
});
socket.bind(8000);
OR:
var socket = dgram.createSocket('udp4');
socket.bind(8000, undefined, function() {
socket.setBroadcast(true);
});
It seems like the file descriptor is created only on bind or on send, and it's required before setBroadcast. You can call client.bind() with no parameter to bind to a random port before setting broadcast. Don't worry about using a random port, since it's done "lazily" when using client.send anyway.
var sys = require('util');
var net = require('net');
var dgram = require('dgram');
var message = new Buffer('message');
var client = dgram.createSocket("udp4");
client.bind();
client.setBroadcast(true);
client.send(message, 0, message.length, 8282, "192.168.1.255", function(err, bytes) {
client.close();
});

Resources