Skip to content Skip to sidebar Skip to footer

Node.js Net Library: Getting Complete Data From 'data' Event

I've searched around, and either can't find the exact question I'm trying to answer, or I need someone to explain it to me like I'm 5. Basically, I have a Node.js script using the

Solution 1:

You shouldn't do anything with the data you recieve, until you receive the end event. The end callback means that all data chunks have been sent through the stream to your callbacks. If data comes in more than one chunk, you need to create a variable within your function closure to store this data to. Most programs can work just fine ignoring this fact, because data usually comes across in one chunk. But sometimes it doesn't. It doesn't even necessarily depend on the amount of data. If you're in a situation where this is happening, I created an example that demos how to handle it. I basically used your code, but removed all the fluff... this is just demoing the logic you need to collect all the data and do work on it.

function connectToServer(tid, ip) {
    var conn = net.createConnection(23, ip);
    var completeData = '';

    conn.on('connect', function() {
        conn.write (login_string);  // login string hidden in pretend variable
    });
    conn.on('data', function(data) {
        completeData += data;
        var dataArray = completeData.split('your delimiter');
        if(dataArray.size > 1) { //If our data was split into several pieces, we have a complete chunk saved in the 0th position in the array
            doWorkOnTheFirstHalfOfData(dataArray[0]);
            completeData = dataArray[1];// The second portion of data may yet be incomplete, thise may need to be more complete logic if you can get more than one delimeter at a time...
        }
    });
    conn.on('end', function() {
        //do stuff with the "completeData" variable in here.
    });
}

Solution 2:

My problem was a logic problem. I was either looking for the chunk that began the message, or the chunk that ended the message, and ignoring everything in between. I guess expected the entirety of the reply to come in in one or two chunks.

Here's the working code, pasted from above. There's probably a more Node-ish way of doing it (I should really emit an event for each chunk of information), but I'll mark this as the answer unless someone posts a better version by this time tomorrow.

function connectToServer(tid, ip) {
        var conn = net.createConnection(23, ip);

        var completeData = '';

        conn.on('connect', function() {
                conn.write (login_string);
        });
        conn.on('data', function(data) {
                var read = data.toString();

                if (read.match(/Login Successful/)) {
                        console.log ("Connected to " + ip);

                        conn.write(command_string);
                }
                else {
                        completeData += read;
                }

                if (completeData.match(/Command OK/)) {
                        if (completeData.match(/\r\nEND\r\n/)) {
                                console.log("Response: " + completeData);
                        }
                }
        });
        conn.on('end', function() {
                console.log("Connection closed to " + ip );
        });
        conn.on('error', function(err) {
                console.log("Connection error: " + err + " for ip " + ip);
        });
}

Post a Comment for "Node.js Net Library: Getting Complete Data From 'data' Event"