Callback function gets called only once - node.js

I'm writing a ZooKeeper client to monitor a node. The callback function gets called only the first time I change the data of the node. I'm not sure why the function doesn't execute on the second change.
A second query is that my program terminates if I uncomment the last close() function. As a good practice, I should use the close() function but then it doesn't enter the blocking state to listen to the incoming events. How do I achieve it? I've read the documentation but couldn't find anything useful.
var zookeeper = require("node-zookeeper-client");
var client = zookeeper.createClient("192.168.43.172:2181");
var path = process.argv[2];
client.once("connected", function() {
console.log("Connected to the server.");
client.create(path, new Buffer("data"), function(error) {
if (error) {
console.log("Failed to create node: %s due to: %s.", path, error);
} else {
console.log("Node: %s is successfully created.", path);
}
});
client.getData(
path,
function(event) {
console.log("Got event: %s.", event);
},
function(error, data, stat) {
if (error) {
console.log(error.stack);
return;
}
console.log("Got data: %s", data.toString("utf8"));
}
);
//client.close();
});
client.connect();

I still don't have an answer to my second query but for the first query, the zookeeper server is designed to send an event only the first time a change takes place and then delete the switch. In order to keep receiving the events, I had to set a watch again while handling the triggered event. Below is my code:
var zookeeper = require("node-zookeeper-client");
var client = zookeeper.createClient("192.168.43.172:2181");
var path = process.argv[2];
client.once("connected", function() {
console.log("Connected to the server.");
var watch = function(event) {
console.log("Got event: %s.", event);
client.getData(path, watch, getDat);
};
var getDat = function(error, data, stat) {
if (error) {
console.log(error.stack);
return;
}
console.log("Got data: %s", data.toString("utf8"));
};
client.getData(path, watch, getDat);
// client.close();
});
client.connect();

Related

nodejs-serialport => RE-Establish connection to port after closed

Accordding to my last question SerialPort 'close' event never fire. I was unabled to detected if the COM is disconnected so I have created my own way to detect it.
I have created timestamp, and checked it with interval() every 1 sec to see if it is connected.
when it's detect the COM is unplugged I have try to re-establish the connection or re-instance port with SerialPort like you'll see inside the code below.
When it's try to reconnect I've get Error: Access denied.
There is a way to refresh or clean the cache? , because I think the server still hold the connection when isn't closed propely.
I've also tried port.close() and it's throw me out: Error: Port is not open.
var comPort = '\\\\.\\COM7',
lastDataTime,
lastresult,
count = 0,
lastDataTime,
comStatus,
error;
var port = new SerialPort(comPort, function (err) {
if (err) {
comStatus = false;
return console.log('Error: ', err.message);
}
});
const parser = port.pipe(new Readline());
port.on('open', function () {
console.log('~Port is open.');
parser.on('data', function (data) {
comStatus = true;
lastDataTime = Date.now();
if (++count == 10) {
count = 0;
lastresult = data;
}
});
});
setInterval(function () {
if (Date.now() - lastDataTime > 1000 || !comStatus) {
comStatus = false;
port.close();
port = new SerialPort(comPort, function (err) {
if (err) {
error = 'Error: ' + err.message;
return console.log(error);
}
});
}
}, 1000);
app.get('/', function (req, res) {
res.send((comStatus) ? lastresult : 'Disconnected - ' + error);
console.log(lastresult);
})
Thanks!
As you can see in /node_modules/serialport/lib/serialport.js: close-event may not be emitted (unlike disconnect).
You can add console.log locally like below to simple debug.
P.S. I tested it on Win7x32. Close-event is emitted.
SerialPort.prototype._disconnected = function(err) {
this.paused = true;
this.emit('disconnect', err);
// add: console.log('1', this.closing);
if (this.closing) {
return;
}
// add: console.log('2', this.fd);
if (this.fd === null) {
return;
}
this.closing = true;
if (process.platform !== 'win32') {
this.readable = false;
this.serialPoller.close();
}
// add: console.log('3');
SerialPortBinding.close(this.fd, function(err) {
// add: console.log('4', this._events.close.toString());
this.closing = false;
if (err) {
debug('Disconnect close completed with error: ', err);
}
this.fd = null;
this.emit('close'); // it's your target
}.bind(this));
};
Reconnect example
var SerialPort = require('serialport');
var port = new SerialPort('COM1', {autoOpen: false, baudRate: 9600});
function open () {
port.open(functon (err) {
if (!err)
return;
console.log('Port is not open: ' + err.message);
setTimeout(open, 10000); // next attempt to open after 10s
});
}
port.on('open', function() {
function send() {
if (!port.isOpen()) // v5.x require
return console.log('Port closed. Data is not sent.');
port.write(123, function (err) {
if (err)
console.log('Error on write: ' + err.message)
port.drain(() => console.log('DONE'));
});
}
setInterval(send, 1000);
});
port.on('close', function () {
console.log('CLOSE');
open(); // reopen
});
port.on('data', (data) => console.log('Data: ' + data));
port.on('error', (err) => console.error('Error: ', err.message));
open(); // open manually
According to the serialport.io,
The resume() method causes an explicitly paused, Readable stream to
resume emitting 'data' events, switching the stream into flowing mode.
Simply, when port is closes, serialport library emits a close event
serialport.on('close', function(error){
if(error.disconnected === true){
console.log("disconnected");
}
}
, which will allow us whether port is disconnected or not.
That means the disconnected port is not available to re-establish the connection again, so you have to use serialport.resume() method to re-enable the connection.
serialport.on('close', function(err){
console.log("Port closed.");
if(err.disconnected === true){
console.log("Disconnected!");
serialport.resume(function(e){
reconnectDevice(); // Serial Port Initialization Function. It's your method to declare serial port.
console.log("Error on resuming port:", e);
});
}
});
After that, it will automatically switch COM ports and you won't get error as 'Port Access denied.'.

express-ws how to periodically check a custom event and take action automatically

I am using express-ws https://www.npmjs.com/package/express-ws (API which helps creating server for express and websocket clients).
app.ws('/', function(ws, req) {
console.log("New connection")
if (content.length > 0) {
console.log(content)
ws.send(content)
}
ws.on('message', function(msg, flags) {
console.log("Received "+ msg);
});
ws.on('data', function(msg, flags) {
var data = []; // List of Buffer objects
res.on("data", function(chunk) {
data.push(chunk); // Append Buffer object
console.log(data)
})
})
});
Now as you can see with code above, whenever a connection is created it checks length of content and sends conetent to client if more than 0.
Following router code, on web request, updates the file.
Issue with this if sometime after connection creation if this file was modified, this connection doesn't know about it and hence send function is not called.
I also tried fs.watch but i am not able to make it to work.
router.post('/run_restart', function(req, res, next) {
text = '{"to_do": "run_test", "devices":"all", "argv": { "test": "' + req.body.cmd + '", "cycles": "' + req.body.cycles + '", "awake_for": "' + req.body.wt + '" }}'
path = process.env['HOME']+'/Desktop/automation/Stressem/StressemWeb/bin/task.txt'
fs.writeFile(path, text)
res.render('home.jade', { title: 'Stressem' });
});
fs.watch(file, function (event) {
fs.stat(file, function (err, stats) {
if(stats.size>80){
console.log("Event: " + event);
fs.readFile(file, 'utf8', function (err, data) {
if (err) throw err;
content = data.toString();
});
}
});
What i would like is whenever the file is updated, ws.send can be called for one of the websocket connection.
Since your server is the one that changes the file, there's no need to use fs.watch as you already know when a file changes. All that's left to do is iterate over a list of open connections and send them the new contents.
var connections = []; // Keeps track of all connections
app.ws('/', function(ws, req) {
console.log("New connection")
connections.push(ws); // Add the new connection to the list
if (content.length > 0) {
console.log(content)
ws.send(content)
}
ws.on('message', function(msg, flags) {
console.log("Received "+ msg);
});
ws.on('data', function(msg, flags) {
var data = []; // List of Buffer objects
res.on("data", function(chunk) {
data.push(chunk); // Append Buffer object
console.log(data)
})
})
// TODO: Make sure you remove closed connections from `connections`
// by listening for the ws `close` event.
});
router.post('/run_restart', function(req, res, next) {
text = '{"to_do": "run_test", "devices":"all", "argv": { "test": "' + req.body.cmd + '", "cycles": "' + req.body.cycles + '", "awake_for": "' + req.body.wt + '" }}'
path = process.env['HOME']+'/Desktop/automation/Stressem/StressemWeb/bin/task.txt'
fs.writeFile(path, text)
res.render('home.jade', { title: 'Stressem' });
connections.forEach(function(c){
c.send(text); // Send the new text to all open connections
}
});
Please note: this won't work if you have multiple processes or servers, but since you're writing to the local file system instead of a database, I assume this is not a requirement.
This simple code work good with express. If a few delay not a problem for you, you can use this.
setInterval(milisecondsToCheck, checkFunction)
for more
http://www.w3schools.com/jsref/met_win_setinterval.asp
https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setInterval
if you use it like this, you can finish it after your job done:
var timer = setInterval(milisecondsToCheck, checkFunction);
To clear it:
clearInterval(timer);
solved this with something like this
var conn_array = [];
app.ws('/', function(ws, req) {
conn_array.push(ws)
console.log("New connection")
fs.readFile(file, 'utf8', function (err, data) {
if (err) throw err;
content = data.toString();
if (content.length > 0) {
console.log(content.length)
conn_array[0].send(content)
}
});
ws.on('message', function(msg, flags) {
console.log("Received "+ msg);
});
ws.on('data', function(msg, flags) {
var data = []; // List of Buffer objects
res.on("data", function(chunk) {
data.push(chunk); // Append Buffer object
console.log(data)
})
})
});
function readFile(){
console.log("I am here")
fs.readFile(file, 'utf8', function (err, data) {
if (err) throw err;
content = data.toString();
if (content.length > 0 && conn_array.length>0) conn_array[0].send(content);
})
}
var interval = setInterval(readFile, 100000);
for now i have assumed there is just one client

Using NodeJS and async.queue to download and save lots of images to local disk

OK I have a NodeJS app and I'm trying to download lots of images from a web server (about 500 for now but the number will increase). The problem I get is a "Unhandled stream error in pipe Error: EMFILE" because it seems that too much files get opened at the same time.
So I'm trying to use async.queue to process files by batches of 20. But I still get the error.
SomeModel.find({}, function(err, photos){
if (err) {
console.log(err);
}
else {
photos.forEach(function(photo){
var url = photo.PhotoURL;
var image = url.replace('http://someurl.com/media.ashx?id=', '').replace('&otherstuffattheend', '.jpg');
photo.target = image;
var q = async.queue(function (task) {
request
.get(task.PhotoURL)
.on('response', function(response) {
console.log(task.PhotoURL + ' : ' + response.statusCode, response.headers['content-type']);
console.log(task.target);
})
.on('error', function(err) {
console.log(err);
})
.pipe(fs.createWriteStream(task.target));
}, 20);
q.push(photo, function(err) {
if (err) {
console.log(err);
}
});
q.drain = function() {
console.log('Done.')
}
});
}
});
What am I doing wrong ? Many thanks for your time and help.
The problem is that you're creating a new queue for each photo and each queue receives just one photo. Instead, only create a queue once (outside of the forEach()) and push the photo objects to it. You're also missing the callback in your task handler. For example:
var q = async.queue(function(task, cb) {
request
.get(task.PhotoURL)
.on('response', function(response) {
console.log(task.PhotoURL + ' : ' + response.statusCode, response.headers['content-type']);
console.log(task.target);
// the call to `cb` could instead be made on the file stream's `finish` event
// if you want to wait until it all gets flushed to disk before consuming the
// next task in the queue
cb();
})
.on('error', function(err) {
console.log(err);
cb(err);
})
.pipe(fs.createWriteStream(task.target));
}, 20);
q.drain = function() {
console.log('Done.')
};
photos.forEach(function(photo) {
var url = photo.PhotoURL;
var image = url.replace('http://someurl.com/media.ashx?id=', '').replace('&otherstuffattheend', '.jpg');
photo.target = image;
q.push(photo, function(err) {
if (err) {
console.log(err);
}
});
});

socket.io functions not working in callbacks

The issue I am having is that I want the current session to join a room if they pass a database check. If they do pass the check, meaning the query returns a result, it should add the user to a room. However, when I call socket.join("room"), it does not work if its in the database callback function. Here is a SIMPLIFIED version of my code:
var pg = require('pg');
var conString = 'connection_to_postgres_server'
var server = require('http').createServer();
var io = require('socket.io')(server);
var port = 8080;
function defaultOnSuccess(query, result){
console.log("Result of query `", query, "` -> ", result);
}
function defaultOnError(query, err){
console.log("Error of query `", query, "` -> ", err);
}
function runQuery(query, queryParams, onSuccess, onError){
onSuccess = (typeof onSuccess !== 'undefined' ? onSuccess : defaultOnSuccess);
onError = (typeof onError !== 'undefined' ? onError : defaultOnError);
pg.connect(conString, function(err, client, done){
if(err){
onError(query, err);
done();
return;
}
client.query(query, queryParams, function(err, result){
if(err){
onError(query, err);
done();
return;
}
else {
onSuccess(query, result);
done();
return;
}
});
});
}
function listenOn(channel, onNotification, onError){
onError = (typeof onError !== 'undefined' ? onError : defaultOnError);
pg.connect(conString, function(err, client, done){
if(err){
onError(channel, err);
done();
return;
}
client.on('notification', function(msg) {
onNotification(channel, msg);
});
var query = client.query("LISTEN \"" + channel + "\";");
done();
});
}
io.on('connection', function(socket){
runQuery("THIS QUERY SHOULD RETURN EXACTLY ONE RESULT IF THE USER IS VALIDATED", [],
function(query, result){
if(result.rowCount == 1){
console.log("Pre rooms: ", socket.rooms);
socket.join("hello");
console.log("Current rooms: ", socket.rooms);
}
}
);
});
io.on('connection', function(socket){
socket.on('disconnect', function(){
});
});
server.listen(port, function(){
console.log('listening on *:' + port);
listenOn("project_tc_changed",
function(channel, message){
console.log(message);
io.emit("data", message);
}
);
});
When I connect with a client, the output of the "Pre rooms:" and "Current rooms:" log is exactly the same. In addition, the io.emit() in server.listen does not work, even though I know the code is getting called, because the message gets logged.
I know for a fact that the socket.join() call and the io.emit() call are getting reached, they are just not having any effects, and not returning any errors.
The socket.join is working as expected, but due to the asynchronous nature of javascript your console logs are not showing what you expected. In javascript every line of code is ran asynchronously, including your socket.join and console.log. This is why we have to make use of callbacks to see what the environment looks like after a function has completed. socket.join allows for this callback. So to see the room join in action, we simply have to change our 3 lines of code to the following:
console.log("Pre rooms: ", socket.rooms);
socket.join("hello", function(){
console.log("Current rooms: ", socket.rooms);
);
As for your emit; If you believe your emit is being reached and the message variable contains data, your io.emit should work. So without seeing what the client side code looks like it is hard to help solve for this.

Return value, or stop script

Helo,
i create API in Windows Azure Mobile service, In this api script i have function to connect the other service. I have problem how to return value or stop executable my script when i have good answer from service. Function process.exit(1), don't work.
function function1(item,response) {
var buf ='';
var net = require('net');
var HOST = 'xxxx.xxxx.xxxx.xxxx';
var PORT = xxx;
var client = new net.Socket();
client.setTimeout(100000, function() {
console.log("Timeout");
response.send(500, "Timeout");
});
client.connect(PORT, HOST, function() {
client.write(item + "\n");
client.on('data', function(data) {
buf = buf + data.toString('utf-8');
});
client.on('close', function() {
});
client.on('end', function() {
if (buf.length > 1) {
var result = JSON.parse(buf);
//if resulr.Aviable is true the functios should return result or send result and stop execiuting script
if ( result.Avaiable) {
response.send(200, result);
//now i wont't to respond answer to client or return my value(result)
console.log('Send data');
}
}
client.destroy();
});
});
}
One alternative is to have a flag which indicates whether a response has been sent or not. This way, when the first of the alternatives is reached, you can set the flag to true (possibly clearing the timeout so it doesn't linger more than it needs) and in all cases check whether the flag has been set before returning the response. Something along the lines of the code below:
function function1(item,response) {
var buf = '';
var net = require('net');
var HOST = 'xxxx.xxxx.xxxx.xxxx';
var PORT = xxx;
var client = new net.Socket();
var responseSent = false;
var timeoutHandler = client.setTimeout(100000, function() {
if (!responseSent) {
responseSent = true;
console.log("Timeout");
response.send(500, { error: "Timeout" });
}
});
client.connect(PORT, HOST, function() {
client.write(item + "\n");
client.on('data', function(data) {
buf = buf + data.toString('utf-8');
});
client.on('close', function(had_error) {
if (!responseSent) {
clearTimeout(timeoutHandler);
responseSent = true;
console.log('Socket closed');
response.send(500, { error: had_error ? 'Socket error' : 'unknown' });
}
});
client.on('end', function() {
if (!responseSent) {
responseSent = true;
clearTimeout(timeoutHandler);
if (buf.length > 1) {
try {
var result = JSON.parse(buf);
if (result.Available) {
response.send(200, result);
} else {
response.send(500, { error: 'Socket data is not available' });
}
} catch (ex) {
response.send(500, { error: 'error parsing JSON', exception: ex });
}
} else {
// We should always return a response
response.send(500, { error: 'No data read from socket' });
}
}
client.destroy();
});
});
}
Notice that since node.js runs on a single thread, you can assume that there will be no cases where the response is sent twice. Also, you should make sure that the response is always sent once - in the code you had, if there was a socket error, or if buf.length was not greater than 1, or if result.Avaiable was not true, then the timeout response would be sent, but you didn't need to wait for the whole (100 seconds) time to send that response.

Resources