So I am answering my own question because I found out about js WebSockets, and the WebSocket-Node lib - they allow full-duplex communication beetween server, and client, and are simple to use. here is server code:
const nwss = require('websocket').server;
const http = require("http");
var server = http.createServer(function(request, response) {
console.log(' Received request for ' + request.url);
response.write("example")
response.end();
});
server.listen(8000, ()=>{console.log("ready")})
ws = new nwss({
httpServer: server,
autoAcceptConnections: false
})
ws.on('request', (req)=>{
let conn = req.accept('example-protocol', req.origin);
console.log('accepted ' + conn.remoteAddress)
conn.on('message', (msg)=>{
if (msg.type == 'utf8') {
console.log('Recieved utf8 ' + msg.utf8Data);
conn.sendUTF(`thanks for "${msg.utf8Data}"`)
}
})
conn.on('close', ()=>{
console.log(`${conn.remoteAddress} disconnected.`)
})
})
(it is a bad idea to allow other sites to connect, so use an if statement to check if the origin is ws://yoursiteurl/)
And client:
let socket_url = "ws://" + new URL(location.href).host + "/"
let WSocket = window['MozWebSocket'] ? MozWebSocket : WebSocket;
let socket_ = new WSocket(socket_url, 'example-protocol') //the example-protocol is the same i put in the server code, as it has to be.
function start() {
socket_.onmessage = (msg)=>{
console.log(msg.data)
}
socket_.send("hi, do you read? :P")
}
//call start once it connects.