Reputation: 85
I have Event listener which triggers by SSE (Server-sent-events),How could i post the data to other server.
NODE JS
//including Event Source
var EventSource = require('eventsource');
var es = new EventSource('http://api.xyz.com:8100/update/');
//Listening URL Event Sourse
es.addEventListener('message', function (e) {
//Extract Json
var extractData = JSON.parse(e.data);
if(extractData.type == 'CALL' )
{
console.log(extractData);
//POST DATA
}
});
i need to post the data to other server other end is PHP
Upvotes: 2
Views: 1511
Reputation: 467
You can try with request library, Related Question: how to make an http post request in node js you can refer this.
//including Event Source
var EventSource = require('eventsource');
var request = require('request'); /*****ADDED******/
var es = new EventSource('http://api.xyz.com:8100/update/');
//Listening URL Event Sourse
es.addEventListener('message', function (e) {
//Extract Json
var extractData = JSON.parse(e.data);
if(extractData.type == 'CALL' )
{
console.log(extractData);
//you can call a function
postDataToServer(extractData);
}
});
//Post Details to other server
function postDataToServer(SendingDetails)
{
var ServerURL = 'OTHER_SERVAL_URL';
request({
url : ServerURL,
method : "POST",
json : true,
body : SendingDetails
}, function (error, response, body){
console.log(body);
});
}
Upvotes: 2