Description
I am using a node server to handle all my push notifications services like GCM and APN.
I have 2 different servers. One is running the Meteor & another is running the Node Js to handle push notifications.
My main application is build on Meteor server.
I make an HTTP post request to nodejs server to send my notifications.
Usually it works fine, but sometimes on Meteor server I get this error whenever I call the nodejs server:
Error: read ECONNRESET at Object.Future.wait (/home/mbm/.meteor/packages/meteor-tool/.1.1.10.12ml1tp++os.linux.x86_64+web.browser+web.cordova/mt-os.linux.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:398:15) at Object.call (packages/meteor/helpers.js:119:1) at Object.sendHttpCall (server/pushNotifications.js:249:1) at server/pushNotifications.js:244:1 at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1) at packages/meteor/timers.js:6:1 at runWithEnvironment (packages/meteor/dynamics_nodejs.js:110:1) - - - - - at errnoException (net.js:905:11) at TCP.onread (net.js:559:19)
Here is my Node Js server code:
realFs = require('fs');
var gracefulFs = require('graceful-fs');
gracefulFs.gracefulify(realFs);
var http = require('http');
var express = require('express');
var app = express();
var path = require("path");
configClass = require('./classes/config.js').configClass;
helperClass = require('./classes/helper.js').helperClass;
pushNotificationClass = require('./classes/pushNotification.js').pushNotificationClass;
var hostname = 'http://localhost';
var port = 6000;
var bodyParser = require('body-parser');
nodeGcm = require('node-gcm');
apn = require('apn');
apnService = new apn.Connection(helperClass.getAPNOptions());
// -- BODY PARSER -- //
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
// All post requests
app.post('/', function(req, res){
try {
var response = JSON.parse(req.body.pushNotificationApiParams);
var callType = req.body.callType;
res.writeHead(200, {'Content-Type': 'text/html'});
switch (callType) {
case 'systemPushNotifications':
return pushNotificationClass.sendPushNotificationsV2(response);
break;
}
res.end('thanks');
}
catch(e){
realFs.appendFile('errorLogs/'+helperClass.getCurrentDateFormated()+'.log', helperClass.formatLog('Exception in main Post Method : '+e.stack) , function (err) {
if (err) throw err;
//console.log('The "data to append" was appended to file!');
});
}
});
app.listen(port);
console.log('Listening at '+hostname+':'+port);
And here is my code from Meteor side, where I am make HTTP post request to node js server:
var headers = {
'Content-Type' : 'application/x-www-form-urlencoded'
};
var postFields = {
callType : 'systemPushNotifications',
pushNotificationApiParams : JSON.stringify(pushNotificationApiParams) // contains push notifications data
};
HTTP.call("POST", 'http://localhost:6000', { params:postFields, headers:headers });
Can anyone guide me in the right direction? Also I would really appreciate to know some good practices as well.