TCP Server + Web Server in the same application

Asked

Viewed 73 times

0

Next guys, I have some ideas for a project and I’m having some questions ...

1º 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 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 folders and files for a tcp server ? Can I apply the MVC concept ? If yes, the type server creation responsibility net.createServer() would be in the controller ?

Thank you, Big hug.

1 answer

1

It is 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 tcp server?

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

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 https://blog.yld.io/2016/02/23/building-a-tcp-service-using-node-js/#. Wtytdwjythe )

If it’s possible, I should do it?

It will depend exclusively on your modeling. You may be wanting to write a compact solution, where listeners HTTP + TCP can co-exist - or a service-separated implementation.

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

Again, the answer depends on your modeling. I confess that I have a certain difficulty in visualizing a folder model for a TCP solution, since we are talking about a stream. If you’re referring to the classes, there’s another chat.

As for 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 host candidate of the connection control features.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.