How to get Nodejs TCP port data in angular component

Viewed 13

Basically, I am developing a desktop application on Electron using node js as the backend and Angular as frontend I have created a server that listens to TCP and prints data in the console now instead of print in the console I want to show this data on the angular component with live update as any message comes to server it should show in angular

below is my node js server code

const express = require("express");
const cors = require("cors");
const net = require('net');


const port = 8888;

const server = net.createServer(onClientConnection);

//Start listening on the given port and host.
server.listen(port,function(){
  console.log(`Server started on port ${port}`); 
});

//the client handling callback
function onClientConnection(sock)
{
  //Log when a client connnects.
  console.log(`${sock.remoteAddress}:${sock.remotePort} Connected`);
  
//Handle the client data.
  sock.on('data',function(data){
      //Log data received from the client
      console.log(`>> data received : ${data} `);
  
  //prepare and send a response to the client 
  let serverResp = "Hello from the server"
  sock.write(serverResp);
  
  //close the connection 
  sock.end()        
});
  
//Handle when client connection is closed
  sock.on('close',function(){
      console.log(`${sock.remoteAddress}:${sock.remotePort} Connection Closed`);
  });
  
//Handle Client connection error.
  sock.on('error',function(error){
      console.error(`${sock.remoteAddress}:${sock.remotePort} Connection ERROR ${error}`);
  });
};```

0 Answers
Related