feathersjs-ecosystem / feathers-sync

Synchronize service events between Feathers application instances
MIT License
222 stars 41 forks source link

Real-time events doesn't fire when using Mongodb database adapter #26

Closed StanleyAM closed 8 years ago

StanleyAM commented 8 years ago

Hi, the below is server side code:

const feathers = require('feathers');
const bodyParser = require('body-parser');
const rest = require('feathers-rest');
const socketio = require('feathers-socketio');
const memory = require('feathers-memory');
const sync = require('feathers-sync');
const MongoClient = require('mongodb').MongoClient;
const service = require('feathers-mongodb');

const app = feathers();
app.configure(rest());
app.configure(socketio());
app.configure(sync({
    db: 'mongodb://XXXX.mlab.com',
    collection: 'events'
}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use("localMemory", memory());

MongoClient.connect('mongodb://XXXX.mlab.com).then(function(db){
    app.use('/mongoDB', service({
        Model: db.collection('syncData')
    }));

    app.listen(8888);
});

When I create new data for both "mongoDB" and "localMemory", there are only "localMemory" firing the created event to client. But data actually is created at mongo database.

The client library uses 1) feathers-client v1.5.1, 2) socket-io v1.4.5 The server package version info is like below:

body-parser@1.15.2
feathers@2.0.1
feathers-memory@0.7.4
feathers-mongodb@2.4.2
feathers-rest@1.4.3
feathers-socketio@1.4.1
feathers-sync@0.1.1
mongodb@2.2.4

Thanks.

P.S. like #11. When I remove feathers-sync. The "mongoDB" real-time events works perfectly.

daffl commented 8 years ago

What is the output when running with the DEBUG=feathers-sync* environment variable set?

StanleyAM commented 8 years ago

Hi daffl,

Thanks your apply.

The log is like below:

feathers-sync will sync via adapter: mongodb  +0ms
feathers-sync setting up database mongodb://XXXX.mlab.com +2ms
feathers-sync subscribing to handler localMemory created +51ms
feathers-sync subscribing to handler localMemory updated +1ms
feathers-sync subscribing to handler localMemory removed +1ms
feathers-sync subscribing to handler localMemory patched +0ms
feathers-sync emitting event to channel localMemory created +16s
feathers-sync emitting event to channel mongoDB created +172ms
feathers-sync got event, calling old emit localMemory created +3ms

Thanks.

daffl commented 8 years ago

Is that the whole log? It looks like it's only subscribing to localMemory events. Can you try if

const feathers = require('feathers');
const bodyParser = require('body-parser');
const rest = require('feathers-rest');
const socketio = require('feathers-socketio');
const memory = require('feathers-memory');
const sync = require('feathers-sync');
const MongoClient = require('mongodb').MongoClient;
const service = require('feathers-mongodb');

MongoClient.connect('mongodb://XXXX.mlab.com').then(function(db){
  const app = feathers();
  app.configure(rest());
  app.configure(socketio());
  app.configure(sync({
      db: 'mongodb://XXXX.mlab.com',
      collection: 'events'
  }));
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({ extended: true }));
  app.use("localMemory", memory());
  app.use('/mongoDB', service({
      Model: db.collection('syncData')
  }));

  app.listen(8888);
});

Makes a difference?

StanleyAM commented 8 years ago

Hi daffl,

Yes, it can work perfectly when configuring feathers' app in the callback of MongoClient's connection.

Thanks a lot.

daffl commented 8 years ago

I wonder why that is. But if it works that way we'll leave it for now.