Connection via socket between servers

1

I have studied communication between two (or more) servers raised in NodeJS via REST más as I searched for content (references, explanations, benchmarks, etc.) I found indications of REST would bring more cost to the process and WebSocket would be more recommended, but all material I found was referring to client (front-end) for server (backend).

The very succinct question is: How to connect and exchange messages between two servers via sockets (backend only)?

The initial implementation where I used REST worked with express to map routes and superagent to make requests.

Now when I try to use WebSocket I'm using the uws module

example.js

var WebSocketServer = require('uws').Server;
var wss = new WebSocketServer({ port: 3000 });

function onMessage(message) {
    console.log('received: ' + message);
}

wss.on('connection', function(ws) {
    ws.on('message', onMessage);
    ws.send('something');
});
    
asked by anonymous 05.11.2017 / 14:15

1 answer

0
  

The very succinct question is: how to connect and exchange messages between two servers via sockets (backend only)?

In the same way that you communicate through the front end, just open the socket server connection and swap messages.

The template may vary:

 -------  ---->  -------------
|backend|       |socket server|
 -------  <----  -------------

 -------  ---->  -------------  ---->  -------
|backend|       |socket server|       |backend|
 -------  <----  -------------  <----  -------

I honestly doubt that "REST would bring more cost to the process", you will have more work to manage a whole system based on sockets than a simple http rest.

    
05.11.2017 / 14:31