File synchronization with node.js: unision == tcp == node ----- [http] ----- node == tcp == unison

If your node is running and node is running, has anyone seen a solution that will allow us to synchronize a set of files.

Synchronization is complicated, so I thought that you could leave it in the form of a unison tool (which is similar to rsync) and then all node should connect TCP between authenticated users.

filesystem1---unision==tcp==node.js------[http]----node.js==tcp====unison---filesystem2

This is probably about 12 lines of JavaScript, but at this point it is outside of me, or any of the examples So far I could find.

I looked at a bunch of other file synchronization options (e.g. Git, veracity, fossil, including a week of trying to install Simias iFolder Server on Linux, crash ~, which looked promising because it included a client file for each main OS) but now I I think that something much, much easier is likely to go.

If someone saw the Node.js project that does this, or is at a level where connecting two TCP channels is not too hard, then I would be glad to hear from you.

+5
source share
1 answer

The main approach I would take to this problem is to use stream2.
My basic approach would be this.

getting data from the first tcp

var gulp = require('gulp')
, thr = require('through2')
;


gulp.watch('rootDirectory/**/*', function(evt) {


  if ('deleted' === evt.type) {
    // do stuff if file is deleted
    return
  }

  // file got changed, moved or added.
  gulp.src(evt.path).pipe(thr(chunk, enc, next){
    // chunk is a vinyl filesytem object, 
    // so better fix chunk before sending to tcp server
    next(null, fixedChunk)

  }).pipe(toYourTcpServer)

})

then on your node

var net = require('net')
, http = require('http')
;

var clientTcp = net.connect({port: 'toYourTcpServerPort'})

var server = http.createServer(function(req, res){
  clientTcp.pipe(res)
}).listen('somePort')

node

var clientTcp2 = net.connect({port: 'toYourTcpServerPort2'})

var server2 = http.createServer(function(req, res){
  // fix response or something else.
  req.pipe(clientTcp2)
}).listen('somePort')

tcp

gutil = require('gulp-util')


clientTcp2.pipe(thr(function(chunk, enc, next){
  // You must create a vinyl filesystem before
  // sending down stream. Use new gutil.File()
  next(null, fixedChunk)

})).pipe(gulp.dest('finalDirectoy'))

through2 , gulp . - , gulp.

, ,

0

All Articles