0

I have created a web server architecture in nodejs which serves web pages when running. On the other hand I have a Qt GUI application. I need to make an interface between these two. I have already created the connection between these two using QProcess (the server now starts from this Qt application) Now I want to exchange some data between them, for example a random message on the web page from GUI and back. Is stdin/out of any use here? Can you please guide me through this? Here is my server code:

 `function init() {

var __website = path.join(__dirname, 'Website/');
var __css = path.join(__dirname, 'css/');

app.use(bodyParser.json());
app.use(express.static(path.join(__website)));
app.get('/', function (req, res, next) {
    res.render('index', function (err, html) {
        res.send(html);
    });
});
app.get('/data/:iValue', function( req, res) {
process.stdin.on('readable', function() {
  var chunk = process.stdin.read();
  if (chunk != null) {
    res.json({response: chunk.toString("UTF-8")});
     }
 });
   process.stdout.write('request_data:' + temperature);

});
}
function initSocket() {
var Session = require('express-session');
var MemoryStore = require('session-memory-store')(Session);
var session = new Session({
 store: new MemoryStore(),
 secret: 'supersecret',
 resave: true,
 saveUninitialized: true
})

//io = require('socket.io')(https);
io = new ioServer();
io.use(ios(session));
// In-memory storage
// Begin listening for a websockets connection
io.on('connection', function (socket) {
    socket.emit('setRange', data.min, data.max);
    socket.emit('setEmails', data.emails);
    setInterval(function () {
    var temperature;
        //var temperature = Math.floor((Math.random() * 20) + 15);
        socket.emit('updateTemperature', {
            temperature: temperature
        });
        if (temperature < data.min || temperature > data.max) {
            // console.log('alert! ' + temperature + ' min:' + data.min + ' 
max:' + data.max)
            sendEmail(temperature);
            socket.emit('alert', {
                temperature: temperature,
                message: 'Temp is outside of Value Range'
            });
        }
    }, 2000);`
  • Please provide a [Minimal, Complete, and Verifiable](https://stackoverflow.com/help/mcve) example of your problem – Simon Apr 02 '18 at 08:41
  • Try to use [qwebsocket](http://doc.qt.io/qt-5/qwebsocket.html) – Simon Apr 02 '18 at 12:22

2 Answers2

0

I think you do not have to make the server be a child process of the GUI program and vise versa.

You need some IPC(Inter-process communication)mechanism here, such as shared memory or named pipe.

And the Socket which is usually for network programming can be used to make two processes communicatable too.

(I just found that node.js doesn't have FIFO(named pipe) or shared memory packages. So, It seems that the only way you can achieve your demand is using Socket.)

H.C.Liu
  • 156
  • 1
  • 4
0

Yes, QProcess can grip stdout, stderr. you can listen to its output with readyReadStandardOutput signal emitted by the process then read by any of qprocess read methods like readAllStandardOutput().. also you can have your gui send messages to nodejs standard input with QIODevice::write(const char *data) inherited function.

Mohammad Kanan
  • 4,452
  • 10
  • 23
  • 47
  • Look at this example to get started [QProcess example](https://stackoverflow.com/questions/49069807/passing-giving-commands-to-cmd-through-qt-gui/49074579#49074579), the thing is nodejs, be able to read or write back to std outpout/err. – Mohammad Kanan Apr 02 '18 at 17:29
  • Thank you so much! I will work on it further this way! – Meenakshi Sharma Apr 02 '18 at 17:59
  • Hello Mr. @Mohammad Kanan, I have tried using these functions in my application the application now returns an error in the debug. Now I think I have to make some changes in my server.js where I need to recieve/snd data – Meenakshi Sharma Apr 04 '18 at 09:57
  • Yes, you need server.js to write to stdout. also please don't put code in comments, its not easily readable. – Mohammad Kanan Apr 04 '18 at 10:12
  • for example;my qt application shows a random value every second with variable name "itemp" and I need to send this value to my webpage with id "Act_temp" (at the moment "Act_temp" receives a Math.random value from server.js. For this, what & where exactly should I change my code? – Meenakshi Sharma Apr 04 '18 at 13:43
  • In a layman's term, iI should replace math.random function with "itemp". right? but how to do it? I mean what is the process of replacing this? @Mohammad Kanan – Meenakshi Sharma Apr 04 '18 at 13:45
  • Depends what your server structures can accept, you could try `json` array or text stream, use `QProess::write` – Mohammad Kanan Apr 04 '18 at 16:22
  • you mean to send "itemp" from Qt program, I should use QProcess:: write? but you can't tell how server.js would receive it in place of Math.random ? – Meenakshi Sharma Apr 04 '18 at 18:47
  • You need to try [process.stdin](https://nodejs.org/api/process.html#process_process_stdin) – Mohammad Kanan Apr 04 '18 at 19:06
  • Hey, thank you. So far, when I used myProcess->write("data_from_qt:" + itemp); I am getting the actual temperature value from qt application into my application output. next step is to make server.js receive this itemp value, you suggested stdin, I am not yet clear how to modify the code(on the link process.stdin) to my requirement. How would server.js know that it has to recive itemp value from qt application. I mean does it need for example something in header? – Meenakshi Sharma Apr 05 '18 at 07:26
  • AFAIK, `stdin` can't be interpreted in a predefined way, its just a byte ararry, you need to capture it and qualify to the parameter you want, for example by a reg exp .. or string methods. – Mohammad Kanan Apr 05 '18 at 07:32
  • I have used stdin as you suggested, but I am still not receiving the value on my web page. Please refer to the code in my question. What am I missing or doing wrong? – Meenakshi Sharma Apr 05 '18 at 11:37