Socket connection between servers

Asked

Viewed 336 times

1

I have studied the communication between two (or more) servers raised in Nodejs via REST 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 however, all the material I found was related to client (front-end) to server(back-end).

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

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

Now when trying to use WebSocket I’m using the module uws

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');
});

1 answer

0

The very brief question is: how to connect and exchange messages between two servers via "sockets" (back-end only)?

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

The model may vary:

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

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

I sincerely 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.

Browser other questions tagged

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