I have the following senario where I would like to send a boolean value back to my component in React. I am able to emit and call my method inside my server.js, however I can't figure out how to send data back once it executes.
Server.js:
...
const io = require('socket.io')(server);
var fetch = () => {
url = 'abc';
request(url, function(error, response, html){
...
fs.writeFile(__dirname + '/data/output.json', JSON.stringify(result, null, 4), function(err){
if(err) {
throw err;
} else {
console.log('File successfully written! - Check your project directory for the output.json file');
io.on('connection', (socket) => {
console.log('send data back to component');
});
}
});
})
}
io.on('connection', (socket) => {
//console.log('a user connected');
socket.on('disconnect', () => {
console.log('user disconnected');
});
socket.on('fetchData', () => {
fetch();
});
});
Component.js (react)
...
handleClick() {
socket.emit('fetchData');
}
render() {
return (
<div>
<button onClick={(e) => this.handleClick(e)}>Fetch</button>
</div>
)
}