pouchdb / pouchdb-server

CouchDB-compatible server built on PouchDB and Node
Apache License 2.0
948 stars 154 forks source link

Live replication not working between two servers #430

Open hadrien-toma opened 4 years ago

hadrien-toma commented 4 years ago

Hello folks,

While running the following two pouchdb-servers, the initial state of localhost:4444/local database is well replicated to localhost:5555/local but then, when I add a document in localhost:4444/local via fauxton, it is not replicated... Does someone have an idea why?

const envs = { admin: { password: 'cloud', username: 'cloud' }, databases: { local: { auth: { username: 'cloud', password: 'cloud' }, subPath: '.dbs/cloud/' } }, port: 4444, subPath: '.dbs/cloud/' }; const prefix = ${process.env.INIT_CWD}/${envs.subPath};

const app = express();

const pouchdbOnFs = PouchDBStatic.defaults({ prefix }); const expressPouchdbOptions = { configPath: ${prefix}/config.json, logPath: ${prefix}/log.txt }; const pouchdbOnFsHandle = expressPouchDB(pouchdbOnFs, expressPouchdbOptions);

pouchdbOnFsHandle.couchConfig.set('admins', envs.admin.username, envs.admin.password, (error) => { console.log('cloud', "couchConfig.set('admins', ...)", { error }); });

app.use('', pouchdbOnFsHandle);

const localDatabase = new PouchDBStatic('local', { prefix: envs.databases['local'].subPath, auth: envs.databases['local'].auth });

localDatabase .put({ _id: 'en', flag: '🇬🇧' }) .then(({ id, ok, rev }) => { console.log('cloud', 'localDatabase', 'put', 'then', { _id: id, ok, rev }); }) .catch((error) => { console.error('localDatabase', 'put', 'catch', { error }); });

const server = app.listen(envs.port);

server.on('error', (error) => { console.log('cloud', 'server', 'error', { error }); });

- localhost:5555:
```js
import * as express from 'express';
import * as expressPouchDB from 'express-pouchdb';
import * as PouchDBStatic from 'pouchdb';

const envs = {
    admin: {
        password: 'device',
        username: 'device'
    },
    databases: {
        local: {
            auth: {
                username: 'device',
                password: 'device'
            },
            subPath: '.dbs/device/'
        },
        remote: {
            name: 'http://localhost:4444/local',
            options: {
                auth: {
                    username: 'cloud',
                    password: 'cloud'
                }
            }
        }
    },
    port: 5555,
    subPath: '.dbs/device/'
};
const prefix = `${process.env.INIT_CWD}/${envs.subPath}`;

const app = express();

const pouchdbOnFs = PouchDBStatic.defaults({ prefix });
const expressPouchdbOptions = {
    configPath: `${prefix}/config.json`,
    logPath: `${prefix}/log.txt`
};
const pouchdbOnFsHandle = expressPouchDB(pouchdbOnFs, expressPouchdbOptions);

pouchdbOnFsHandle.couchConfig.set('admins', envs.admin.username, envs.admin.password, (error) => {
    console.log('device', "couchConfig.set('admins', ...)", { error });
});

app.use('', pouchdbOnFsHandle);

const databases: { [key: string]: PouchDB.Database<any> } = {};

databases['local'] = new PouchDBStatic<any>('local', {
    prefix: envs.databases['local'].subPath
});
databases['remote'] = new PouchDBStatic<any>(envs.databases['remote'].name, envs.databases['remote'].options);

databases['local'].replicate.from(databases['remote'], { live: true, retry: true });

const server = app.listen(envs.port);

server.on('error', (error) => {
    console.log('device', 'server', 'error', { error });
});
BillMeyerRSA commented 4 years ago

Hello folks,

While running the following two pouchdb-servers, the initial state of localhost:4444/local database is well replicated to localhost:5555/local but then, when I add a document in localhost:4444/local via fauxton, it is not replicated... Does someone have an idea why?

  • localhost:4444:
import * as express from 'express';
import * as expressPouchDB from 'express-pouchdb';
import * as PouchDBStatic from 'pouchdb';

const envs = {
  admin: {
      password: 'cloud',
      username: 'cloud'
  },
  databases: {
      local: {
          auth: {
              username: 'cloud',
              password: 'cloud'
          },
          subPath: '.dbs/cloud/'
      }
  },
  port: 4444,
  subPath: '.dbs/cloud/'
};
const prefix = `${process.env.INIT_CWD}/${envs.subPath}`;

const app = express();

const pouchdbOnFs = PouchDBStatic.defaults({ prefix });
const expressPouchdbOptions = {
  configPath: `${prefix}/config.json`,
  logPath: `${prefix}/log.txt`
};
const pouchdbOnFsHandle = expressPouchDB(pouchdbOnFs, expressPouchdbOptions);

pouchdbOnFsHandle.couchConfig.set('admins', envs.admin.username, envs.admin.password, (error) => {
  console.log('cloud', "couchConfig.set('admins', ...)", { error });
});

app.use('', pouchdbOnFsHandle);

const localDatabase = new PouchDBStatic<any>('local', {
  prefix: envs.databases['local'].subPath,
  auth: envs.databases['local'].auth
});

localDatabase
  .put<any>({ _id: 'en', flag: '🇬🇧' })
  .then(({ id, ok, rev }) => {
      console.log('cloud', 'localDatabase', 'put', 'then', { _id: id, ok, rev });
  })
  .catch((error) => {
      console.error('localDatabase', 'put', 'catch', { error });
  });

const server = app.listen(envs.port);

server.on('error', (error) => {
  console.log('cloud', 'server', 'error', { error });
});
  • localhost:5555:
import * as express from 'express';
import * as expressPouchDB from 'express-pouchdb';
import * as PouchDBStatic from 'pouchdb';

const envs = {
  admin: {
      password: 'device',
      username: 'device'
  },
  databases: {
      local: {
          auth: {
              username: 'device',
              password: 'device'
          },
          subPath: '.dbs/device/'
      },
      remote: {
          name: 'http://localhost:4444/local',
          options: {
              auth: {
                  username: 'cloud',
                  password: 'cloud'
              }
          }
      }
  },
  port: 5555,
  subPath: '.dbs/device/'
};
const prefix = `${process.env.INIT_CWD}/${envs.subPath}`;

const app = express();

const pouchdbOnFs = PouchDBStatic.defaults({ prefix });
const expressPouchdbOptions = {
  configPath: `${prefix}/config.json`,
  logPath: `${prefix}/log.txt`
};
const pouchdbOnFsHandle = expressPouchDB(pouchdbOnFs, expressPouchdbOptions);

pouchdbOnFsHandle.couchConfig.set('admins', envs.admin.username, envs.admin.password, (error) => {
  console.log('device', "couchConfig.set('admins', ...)", { error });
});

app.use('', pouchdbOnFsHandle);

const databases: { [key: string]: PouchDB.Database<any> } = {};

databases['local'] = new PouchDBStatic<any>('local', {
  prefix: envs.databases['local'].subPath
});
databases['remote'] = new PouchDBStatic<any>(envs.databases['remote'].name, envs.databases['remote'].options);

databases['local'].replicate.from(databases['remote'], { live: true, retry: true });

const server = app.listen(envs.port);

server.on('error', (error) => {
  console.log('device', 'server', 'error', { error });
});

Were you able to figure out how to do this? Having similar issues.

hadrien-toma commented 4 years ago

Nop, I am still blocked on this part...