Skip to content

7 Cluster

Alexandre Tiertant edited this page May 16, 2018 · 3 revisions

Overview

synapps application can connect each other via a secure tls/tcp connection and then request other node.

Exemple

In this exemple, we will connect two synapps applications. on client request, first application will send a request to second that will respond to first and then first to client.

Generate Key

to etablish a secure tls/tcp connection, we need to generate a rsa private key and a certificate.

don't worry, Synapps CLI can do this for you. here we generate a 'local' key for localhost ip:

$ synapps cluster genKey local 127.0.0.1
generating keys for local at 127.0.0.1
$ ls
local.crt  local.key

First application

var synapps = require('@synapps/core');
var path = require('path');
var app = synapps();

var certPath = path.join(__dirname, 'local.crt');
var keyPath = path.join(__dirname, 'local.key');

// set node name
app.set('name', 'first');

// set a specific port to avoid conflict
app.set('ipcLocalPort', 8001);

// set tls parameters to use generated key and certificate
app.set('tls', {
  key: keyPath,
  cert: certPath,
  ca: [certPath],
  port: 8101
});

app.route('/', function(req) {
  req.emit('second', '/', function(err, res) {
    if (err) {
      return req.reject(err);
    }
    req.resolve(res);
  });
});

app.listen(3000);

Second application

var synapps = require('@synapps/core');
var path = require('path');
var app = synapps();

var certPath = path.join(__dirname, 'local.crt');
var keyPath = path.join(__dirname, 'local.key');

// set node name
app.set('name', 'second');

// set a specific port to avoid conflict
app.set('ipcLocalPort', 8002);

// set tls parameters to use generated key and certificate
// specify connectTo to connect to first application
app.set('tls', {
  key: keyPath,
  cert: certPath,
  ca: [certPath],
  port: 8102,
  connectTo: [{name: 'first', host:'127.0.0.1', port: 8101}]
});

app.route('/', function(req) {
  req.resolve('hello world');
});

app.listen(3001);
Clone this wiki locally