Datsik
Datsik

Reputation: 14824

How can I have 2 Node apps on different servers that talk to each other?

Basically I have the middle-man webpage which is a website that people can go to, login and create servers (for a game) in different locations, e.g. Texas, New York, Chicago.

Right now I have my main app running on New York

I want to have a sub-app that runs on those servers which is solely responsible for creating child_processes to start servers.

I was playing around with having the sub-apps run off of get/post requests, but trying to send get/post requests from the Node side is just painful, and trying to secure it so other people don't just CURL a request and do something malicious is there a way to maybe do this with sockets or something so that the sub-apps connect to the main socket of the main web-app and then can send out a start emit or something to the sub-apps to create a server? Sorry if this doesn't make sense I'm fairly new to having apps talk to each other.

Upvotes: 4

Views: 2924

Answers (2)

BOUDA wahiba
BOUDA wahiba

Reputation: 96

you can make your two node servers communicate with the websocket node module 'ws'. you can install it by typing this command:

npm install --save ws

To connect your node apps, one node app should be listening on a port (for exapmle: 3001), which make it acting as a server for the other node app :

var webSocketServer = require('ws').Server,
wss = new WebSocketServer ({port: 3001, path: '/path'});
wss.on('listening', function() {
    console.info('ws server is listening')
})

And then you can connect the other node app like this:

var WebSocket = require('ws');
var ws  = new WebSocket('ws://www.yourServerDomain.com:3001/path');

now you can send and receive data from both sides, you can see in this link to see how to do it.

Upvotes: 3

TM.
TM.

Reputation: 3741

I would suggest you to use Message Queues

At the simplest level, a queue messages is a way for applications and discrete components to send messages between one another in order to reliably communicate.

And in NodeJS you can realize queue jobs with kue library

Upvotes: 1

Related Questions