2

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

1
  • if you want to share the socket object across other services , you need to make an common communication point between the services , this is similar to microservices architecture (everything seems to be microservices nowdays). You can pass the socket object via a callback parameter. Best way from my point of view is to keep track in an object ID=>socket in one service and only pass the ID to another service when the 2nd service needs something to be send, just pass the ID back to the main tcp server to broadcast the message. Commented Apr 27, 2017 at 10:07

1 Answer 1

4

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;
};
Sign up to request clarification or add additional context in comments.

3 Comments

so in file "main.js" I can use 'server.socket.write()' or how will it work?
@Mr.Phoenix In file "main.js" I have a lot of functions for buttons, for example. In "server.js" I create tcp server and this file required to main file. When application starts -> client connects to my Server and when I click on button -> I should send something to client
I've edited the response. You can pass your server in parameter at any js file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.