Waiting till call back completes Node.js - javascript

My situation is like this, I have a server like this and inside I am calling another callback function which gets multiple values:
var http = require('http');
var server = http.createServer(req,resp) {
var finalVal = "";
consumer.on('message',function(message) {
finalVal = message;
console.log(finalVal);
});
resp.end(finalVal);
});
My finalVal should display all the multiple values it fetches and send it as a response, but problem is it's sending only first value where as console.log displays all the values. I do understand that by the time consumer.on ends response would have committed. Can someone please help me how to handle this scenario since I'm very new to Node.js ? Currently due to heavy deadlines I don't have time to read full information about callbacks. But defnitely I would take time to learn about callbacks.
Edit: Here consumer.on calls multiple times till it fetches all the data from backend, I need to send all those data in a final response. I am using node-kafka to consume to kafka messages.

There must be an end event or something like that which tells that consumer has finished the message, you can use that
I am not sure what is consumer here, but in most of cases we go with something like bellow
var finalVal = '';
consumer.on('message',function(message) {
finalVal += message; // So on each message you will just update the final value
console.log(finalVal);
});
consumer.on('end',function(){ // And at the end send the response
resp.end(finalVal);
});

Related

TcpClient don't receive any data with ajax call

I'm struggling to find out why TcpClient don't receive any data in server side if it has called through ajax.
However, if I put breakpoint in my server side code it works fine even if I have called it with ajax.
I also investigated to find out if my JavaScript function is asynchronous but it seems my JavaScript function is fine.
JavaScript function:
$('#btnGO').click(function () {
var url = 'Home/Command';
var data = { Location: $('#Location').val() };
$.when($.getJSON(url, data)).then(function (result) {
$('.Console').html(result);
});
});
Server side:
TcpClient tc = new TcpClient("Host Address", 23);
return Json(tc.Connected + " " + tc.Available, JsonRequestBehavior.AllowGet);
Output if I put breakpoint in serverside:
true 22
Output if I don't put breakpoint in serverside:
true 0
I think you'd want to call GetStream(), and then call Read() on the returned NetworkStream. Read() is blocking, and won't allow your action method to return prematurely. Right now, there are no blocking calls to prevent your action method from instantly returning (faster than your tcp client receives data), which is why you get 22 when you put in a break point - it doesn't instantly return. It seems awkward that your UI responsiveness depends on somebody sending data to your API via a socket though....
let me emphasize this more: It's really strange what you're doing. Your UI will be waiting for a client to send data to your API via a socket. Having said that, check out the following link: https://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.getstream(v=vs.110).aspx

How does node.js http.get() "on(end,callback())" event works?

My question is: How does the "http.get()", on.("end",callback) event work?
What is the hierarchy of the code execution?
I'm asking because I have this code
var http = require("http")
var str = ""
http.get(process.argv[2],function(res){
res.setEncoding("utf8")
res.on("data",function(data){
str+= data
})
res.on("end",function(){
console.log(str.length)
console.log(str)
})
})
is the on.end part would print me the str.length every time its called?
To start with,
function(res) is called when the connection is established.
on('data') is called when there's a chunk of data (this almost certainly will be more than once)
on('end') is called when the connection closes.
on('error') is called when there is some sort of error.
This code means that till the data is incoming (in chunks), the response will be appended every time it is received from data to str and when the receiving has ended it will console str.length and str.
You can read this for better understanding: colmsjo.com/130721_Streams_in_NodeJS

js/ nodejs basic beginner server for POST method using request.on() and querystring.parse()

I'm starting to learn some nodejs in my web classes after javascript to make some simple servers, and with this example below:
var http = require('http');
var url = require('url');
var querystring = require('querystring');
function onRequest(request, response) {
if (request.method == 'POST') {
var body = '';
request.on('data', function (data) {
body += data; // data sent via http protocol will always be sent as a string and can be concatenated
// if body > 1e6 === 1* Math.pow(10,6) ~~~ 1MB
// flood attack or faulty client
// (code 413: request entity too large), kill request
if (body.length > 1e6) {
response.writeHead(413, {'Content-Type':'text/plain'}).end();
request.connection.destroy();
}
}); // end of data communication: body will contain all the parameters of the query
request.on('end',function(){
var POST = querystring.parse(body);
// now to get the different parameters use // POST.<field name> e.g. POST.user
response.end('Hello, ' + POST.firstname + ' ' + POST.lastname);
});
}
}
var server = http.createServer(onRequest);
server.listen(3000);
I have understood up to this point but have trouble looking up and understanding the request.on() and querystring.parse() portions of this code. I'll highlight them for more clarity below the exact parts I'm confused with.
1) request.on()
So I've read that request.on('data') will have nodejs set a listener for the event of receiving a chunk of data. So with the example above this part:
request.on('data', function (data) {
body += data;
if (body.length > 1e6) {
response.writeHead(413, {'Content-Type':'text/plain'}).end();
request.connection.destroy();
}
It's taking a second parameter as a callback function that takes the first parameter 'data' again. This is what I'm confused with, what is it trying to do here with the second parameter?
2) request.on('end') and querystring.parse()
I read that request.on('end') will have nodejs set a listener for the signal that the data upload completed, so this code below:
request.on('end',function(){
var POST = querystring.parse(body);
// now to get the different parameters use // POST.<field name> e.g. POST.user
response.end('Hello, ' + POST.firstname + ' ' + POST.lastname);
}
Inside request.on('end') we make a new variable called POSTand set it equal to querystring.parse(body) with body being the previous variable of all the data combined. How does it go from this querystring.parse(body) and apply .firstname on it (POST.firstname) and access that component of it?
Thanks in advance.
For your first question:
It's taking a second parameter as a callback function that takes the first parameter 'data' again. This is what I'm confused with, what is it trying to do here with the second parameter?
So what you're doing here is defining a listener function to be called each time the request fires a data event. You may be able to see it clearer in the following manner:
request.on('data', onChunkReceived)
function onChunkReceived (data) {
body += data
if body > 1e6 === 1* Math.pow(10,6) ~~~ 1MB
if (body.length > 1e6) {
response.writeHead(413, {'Content-Type':'text/plain'}).end();
request.connection.destroy();
}
}
What you're doing is defining a function, which takes a data parameter; don't get too hung up in the fact that the event name and the parameter name that was chosen are the same; there's no reason you couldn't name it whatever you want:
request.on('data', onChunkReceived)
function onChunkReceived (chunk) {
body += chunk
if body > 1e6 === 1* Math.pow(10,6) ~~~ 1MB
if (body.length > 1e6) {
response.writeHead(413, {'Content-Type':'text/plain'}).end();
request.connection.destroy();
}
}
...will act exactly the same way. So again: data is the name of the event, and you're defining a function that receives each chunk of data as the server receives it; in your example above the function is just anonymous function that is in-line, as I've reformatted the snippet above it's a separate named function.
Now to your second question:
Inside request.on('end') we make a new variable called POSTand set it equal to querystring.parse(body) with body being the previous variable of all the data combined. How does it go from this querystring.parse(body) and apply .firstname on it (POST.firstname) and access that component of it?
The querystring.parse method takes a string in a specific format, and parses it into a Javascript object. The Node.js docs on querystring explain it far better than I will be able to here. So, I can only assume that the example you're working from expects that the body will be in a specific format, that is form encoded meaning that it's in the format of the following:
firstname=Dan&lastname=Takahashi
When you call querystring.parse on that string, you'll be returned an object that looks like:
POST = {
"firstname": "Dan",
"lastname": "Takahashi"
}
Which you can then address as you say above: POST.firstname which will be "Dan" and POST.lastname, which will be "Takahashi".
Questions 1 & 2 deal with the http request data stream and allow you to inject your code to do custom activities like save the stream to a buffer/var for later use.
If you want to query the POST parameters you can simply use the bodyparser node module https://github.com/expressjs/body-parser
to query the parameters using object dot notation. For example req.body.form_field_name
body-parser is a nice module to use to get quick access to data passed along in a POST/PUT method.
Express keeps the raw body data hidden and you'll need to use a module similar to body-parser to get access. Please note this is a read only copy of the body content.
Cheers.
PS if body-parser doesn't address your immediate need, check out
github.com/stream-utils/raw-body
github.com/moscartong/express-rawbody

AJAX and a global variable not working, have I got this wrong?

what I am trying to achieve is to AJAX a load of client's data into a page (this works), I then have a company ID in one of the fields brought in. I need to cross check this with a different company table (same database) to replace the company ID on the page with the name instead.
To get this I have set a global javascript variable to blank then fired off the main AJAX request getting all the initial client data then within that parsing loop (client side) I need to fire off a function which will check against the companies table to get the name. My current problem is that the global variable is not being set to the 2nd AJAX result. Here is my code:
var nameresult = "";
function namecheck(id){
var request = new Ajax().sendRequest
('../company_check.php',
{ method: 'GET',
parameters: 'id=' + id,
callback: namecheckReceived }
);
}
function namecheckReceived(xmlHTTP){
var n_data = JSON.parse(xmlHTTP.responseText);
nameresult = n_data[0].name;
}
function client_call(){
var request = new Ajax().sendRequest
('../client_data.php',
{ method: 'GET',
callback: searchReceived }
);
}
function searchReceived(xmlHTTP){
var data = JSON.parse(xmlHTTP.responseText);
for(var i=0; i<data.length; i++)
{
namecheck(data[i].company_id);
/////spit out all the data in a readable format //////
}
}
Notes:
Only one result will be received from the company_check.php hence no
loop in the namecheckRecieved() function.
No errors in the JS console.
The nameresult variable stays as blank and is never
changed, if I alert(nameresult) within the namecheckRecieved()
function it spits out what I want so why is it not changing the
global variable with each loop of the searchRecieved() function?
I'm going to delete all my previous comment and say that you only need one ajax call. And everything should be done on server side. That means get the company Id, and use that to get the name of the company then pass everything back to the client side. From the look of it you are doing A LOT of call backs to the server to get every company name when you could have just done that on your first visit to the server. This way you do not need to worry about doing two ajax call Although from the look of it your doing more than 2 calls, depending on the length of data
Try this
function namecheckReceived(xmlHTTP){
var n_data = JSON.parse(xmlHTTP.responseText);
nameresult = n_data[0].name;
client_call();
}

Javascript Error when updating client using AJAX call

I am trying to model a chat application on a browser(Firefox).Here I am trying to send the char inserted into a text area on one machine to another client and update its text area.
I am trying to send the key pressed on the client using AJAX calls. Below is the function call that I have written :
function returnKeyFromCode(code)
{
//Returns char code
};
function keyPress(e)
{
var textReplace = document.getElementById("textReplace");
var keyPressed = returnKeyFromCode(e.which) ;
textReplace.innerHTML = keyPressed;
var locationReplace = document.getElementById("location");
locationReplace.innerHTML = mainDoc.selectionStart;
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function(){};
xmlhttp.open("POST","http://localhost:5000?key=" + keyPressed + "&pos=" +mainDoc.selectionStart +"&revno=1&param=EOS",true);
xmlhttp.send("Stuff");
};
At the client side, when the char is received the following error is displayed on the fire bug console:
'0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIXMLHttpRequest.status]' .
Before sending the data, a persistent connection is being set up between client ans server using another Ajax call.
It looks like you've edited some of your code before posting this question, so I can only speculate, but based on the error, i'd guess at some point you're sending null to this function: xmlhttp.send("Stuff");
Try doing a null check on the data before you send it, or possibly make sure whatever starts the send process is in a document ready, so that you're not trying to grab data from an undefined text element.

Categories