Curious101
Curious101

Reputation: 1748

pass json from one node server to another - most efficient way?

I am an intermediate level in node. I need to pass JSON from one node.js server to another node.js (both servers are independant of each other and meant for different functions). The JSON data can be between 200 bytes to 50 KiloBytes. (upto 500 requests for second)

I was using http post but it stopped working over node.js 0.12.7. I cannot downgrade node and run the risk of breaking some other functionality. So I am looking for an alternative method to pass JSON data between the two Servers.

Here is my code for the POST request:

  function contactPushServerToSendMessage(recipients, entireMessage) {

   var post_options = {
       host: '<devServer>.cloudapp.net',
       port: '80',
      path: '/api/'+'sendMessage',
  method: 'POST',
  headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Content-Length': entireMessage.length
    }
  };

 console.log("with body: " + entireMessage);

 // Set up the request
 var post_req = http.request(post_options, function(res) {
                                          res.setEncoding('utf8');
                                          res.on('data', function   (chunk) {
                                                                   console.log('Response: ' + chunk);
                                                  });
                         });

    // post the data
    post_req.write(entireMessage);
    post_req.end();

   }
 }

And here is the error message this returns:

Error: "name" and "value" are required for setHeader().
    at ClientRequest.OutgoingMessage.setHeader (_http_outgoing.js:333:11)
    at new ClientRequest (_http_client.js:101:14)
    at Object.exports.request (http.js:49:10)
    at contactPushServerToSendMessage (/home/azureuser/myAppServer/nodeServer.js:297:23)
    at /home/azureuser/myAppServer/nodeServer.js:65:8
    at Layer.handle [as handle_request] (/home/azureuser/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/azureuser/node_modules/express/lib/router/route.js:131:13)
    at Route.dispatch (/home/azureuser/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/azureuser/node_modules/express/lib/router/layer.js:95:5)
    at /home/azureuser/node_modules/express/lib/router/index.js:277:22

Please advice what will be an efficient and reliable method to do this.

Thanks in advance.

Upvotes: 1

Views: 767

Answers (2)

Risto Novik
Risto Novik

Reputation: 8295

You can always use some queue solutions, for example the AWS SQS, Kinesis etc.

As you have mentioned you could also use the POST request for that, but you get no guarantee for request reaching the destination. If you do want to use this pattern make sure you include some retry logic, the ACK response if the other server have received the message etc. That's why it is quicker to use some service for that.

SQS, queue based solution you need producer and consumer - https://aws.amazon.com/sqs/

Kinesis, allows realtime stream consuming - https://aws.amazon.com/kinesis/

Upvotes: 0

Cristian Smocot
Cristian Smocot

Reputation: 666

Have a look at this blog post:

http://www.sebastianseilund.com/json-socket-sending-json-over-tcp-in-node.js-using-sockets

It details how to pass JSON data by TCP using sockets.

Upvotes: 1

Related Questions