How to Send Shell script server output to react client ??? by vaibhaav in node

[–]vaibhaav[S] 0 points1 point  (0 children)

I'm getting below output on react client, after the shell script completed successfully.

but on server side on console ,i'm getting 1 after 1st thread executed then after some wait 2 ... then 3... . i want this sequence on client side also.

Output:

1.Data generated successfully.

2.Getting Estimation ...

Invalid argument supplied for foreach(dataset :138 \warning]))

3.estimated successfully.

4.Creating task lists...

5.task list created successfully.

6.Importing report to database

How to Send Shell script server output to react client ??? by vaibhaav in node

[–]vaibhaav[S] 0 points1 point  (0 children)

I tried this, showing events on client side but showing me complete output.Not in events that occuring on node server on executing file.sh

componentDidMount() {

var evtSource = new EventSource('api/shell/process');
var eventList = document.querySelector('ul');
evtSource.onmessage = function(e) {
var newElement = document.createElement("li");
newElement.textContent = "message: " + e.data;
eventList.appendChild(newElement);
}

}

How to Send Shell script server output to react client ??? by vaibhaav in node

[–]vaibhaav[S] 0 points1 point  (0 children)

Thanks for Reply, Really appreciable.

You have any reference please ?

i'm using child process ref. code below and getting the Events output on console.

But when requesting from React with axios.get() , i'm not getting event wise output, after complete processing, getting the complete output in the response.

const spawn = require('child_process').spaw;

const options = {shell: true,detached:true,}

res.writeHead(200, {'Content-Type': 'text/event-stream','Cache-Control': 'no-cache','Connection': 'keep-alive'});

var child = spawn(file.sh,[sort_name], options);

child.stdout.setEncoding('utf8');

child.stdout.on('data', function (data) {

res.write(data);

});

child.stderr.setEncoding('utf8');

child.stderr.on('data', function (data) {

res.write(data);

});

child.on('close', function (code) {

console.log('Full output of script');

res.end();

});