Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

243
Views
Node.js click button and send something with TCP/IP protocol

I am new in node. I am trying to make the application where user can click on button and something will go to the client with TCP.
Here is my ECHO server (example from nodejs.org)

const net = require('net');

var HOST = '0.0.0.0'
var PORT = 6969;

var server = net.createServer();
server.listen(PORT, HOST);
console.log('Server listening on ' + server.remoteAddress + ':' + server.remotePort);

server.on('connection', function(socket) {
  console.log('CONNECTED: ' + socket.remoteAddress + ':' + socket.remotePort);
  socket.write('Hello from server');
  // other stuff is the same from here
  socket.on('data', function(data) {
    socket.write('reply data ' + data);
  });
});

Could you explain me how to get this connection in another js file. I am trying make module.exports = server, but I can not understand how to send information from another file. Help me, please

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You could try something like this :

app.js

 var HOST = '0.0.0.0'
 var PORT = 6969;

 var server = net.createServer();
 server.listen(PORT, HOST);
 console.log('Server listening on ' + server.remoteAddress + ':' + 
 server.remotePort);

 const socket = require("./controllers/socket")(server);
 app.get("/socket/connection", socket.connection);

socket.js

const net = require('net');

module.exports = function(server) {

   module.connection = function(request, response) {

     server.on('connection', function(socket) {
       console.log('CONNECTED: ' + socket.remoteAddress + ':' + 
       socket.remotePort);
       socket.write('Hello from server');
       // other stuff is the same from here
       socket.on('data', function(data) {
          socket.write('reply data ' + data);
       });
     });


   return module;
};
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!