I have a node.js
app with websocket
When the user get into '/'
I need to send command message (json message) with web socket to other application.
When I get results from the other application, I need to return it.
It seems that I return empty results to the user, and I don't know why
serv.js
file:
const websocket = require('ws')
async function sendData(command) {
const ws = new websocket('ws://127.0.0.1:5678')
ws.on('open', function open() {
ws.send(JSON.stringfuly(command))
});
ws.on('message', function incoming(data) {
return data
});
return {}
}
module.exports = {
sendData,
}
index.js
file:
connection = require('serv');
const router = Router();
.route('/')
.get(async(req, rep) => {
// fill json command
command = fillCommand()
result = await connection.sendData(command)
res.json(result);
})
I tried to debug, and I can see that results = await connection.sendData(command)
get {}
result
only.
I tried to remove the return {}
and I got the same results
I see that I'm getting results from the ws.on('message'...
, But it is not get back to index.js
what am I missing and how can I fix it ?