TCP Server + Web Server in same application

0

Next person, I have some ideas about a project and I have some doubts ...

1st Is it possible to create a TCP Server that will handle the connection with the Client along with a Web Server using express that will have functions like start, stop and restart of the tcp server? 2nd If possible, should I do this? Would it be a good idea to put the two in the same application or is it better to separate the two? (Taking into account that the tcp server will have many accesses)

3rd Is there an ideal structure for the folders and files for a tcp server? Can I apply the MVC concept? If yes, would the responsibility of creating the server type net.createServer() be in the controller?

Thank you, Big hug.

    
asked by anonymous 06.05.2017 / 05:45

1 answer

1
  

Is it possible to create a TCP Server that will handle the connection with the Client together with a Web Server using express that will have functions like start, stop and restart of the tcp server?

Yes, it is possible. For this you need to initialize a TCP listener:

var net = require('net');

var server = net.createServer();  
server.on('connection', handleConnection);

server.listen(9000, function() {  
  console.log('server listening to %j', server.address());
});

function handleConnection(conn) {  
  // [...]
}

(Partial example of link )

  

2nd If possible, should I do this?

It will depend exclusively on your modeling. You may be wanting to write a compact solution where listeners HTTP + TCP may coexist - or a separate implementation by services.

  

3rd Is there an ideal structure for the folders and files for a tcp server? Can I apply the MVC concept?

Again, the answer depends on your modeling. I confess I have some difficulty in viewing a folder template for a TCP solution, since we are talking about a stream . If you're referring to classes, here's another one.

When to the MVC model, yes, there are several NodeJS modules that adopt this model. Since net.createServer() has to do with state maintenance, I would say yes, the controller layer would be a good candidate to host the connection control features.

    
06.06.2017 / 04:39