Node - send large JSON through a network connector

The problem is that sending large serialized JSON (over 16,000 characters) over a network socket is split into pieces. Each piece fires an event dataon the receiving side. Thus, simply starting JSON.parse()on incoming data may fail SyntaxError: Unexpected end of input.

The work that I still managed to find is to add a null character ( '\u0000') to the end of the serialized JSON and check it on the receiving side. Here is an example:

var partialData = '';
client.on( 'data', function( data ) {
    data = data.toString();
    if ( data.charCodeAt( data.length - 1 ) !== 0 ) {
        partialData += data;
        // if data is incomplete then no need to proceed
        return;
    } else {
        // append all but the null character to the existing partial data
        partialData += data.substr( 0, data.length - 1 );
    }
    // pass parsed data to some function for processing
    workWithData( JSON.parse( partialData ));
    // reset partialData for next data transfer
    partialData = '';
});

One of the failures of this model is that the receiver is connected to several sockets, and each socket sends large JSON files.

, , - , , , . , . , : -, JSON Node.js? -, , , JSON ?

+3
2
  • try... catch , , json. , .
  • json JSON.
  • , JSON. \u0000 - , . .

  • , dnode, , . . .

, , JSON.

. .

+4

. id , , , , .

net.createServer( function(socket) {
  // There are many ways to assign an id, this is just an example.
  socket.id = Math.random() * 1000;
  socket.on('data', function(data) {
    // 'this' refers to the socket calling this callback.
    buffers[this.id] += data;
  });
});

, , "", , .

+1

All Articles