Skip to content

Socket IO not syncing "thing" in default app #2491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task done
EliKrumholz opened this issue Feb 5, 2017 · 5 comments
Closed
1 task done

Socket IO not syncing "thing" in default app #2491

EliKrumholz opened this issue Feb 5, 2017 · 5 comments

Comments

@EliKrumholz
Copy link

EliKrumholz commented Feb 5, 2017

  • I understand that GitHub issues are not for tech support, but for questions specific to this generator, bug reports, and feature requests.
Item Version
generator-angular-fullstack 4.1.2
Node 6.9.5
npm 3.9.5
Operating System OS X 10 and Heroku
Item Answer
Transpiler Babel
Markup HTML
CSS SCSS
Router ui-router
Client Tests Mocha
DB MongoDB
Auth Y
SocketIO Y

"Things" in the main view are not syncing automatically when created or deleted, a full page refresh is required to see the updates. I observed this behavior on both OSX 10.12 and on heroku using default settings for the generator at v4.1.2. Just a default build of the generator deployed to heroku should replicate this bug as far as I can tell.

I am not seeing any errors on either the client side or the server side, any suggestions on where to start tracking down this bug?

@ernistkg
Copy link

ernistkg commented Feb 5, 2017

Problem

I have researched this issue and found reason why this doesn't work.

In thing.events.js file
./server/api/thing/thing.events.js

// Register the event emitter to the model events
for(var e in events) {
  let event = events[e];
  Thing.schema.post(e, emitEvent(event));
}

As you can see inside for loop remove and save events are applied to Thing Schema thorough POST HOOKS . And this hooks never called. I think this happens because we are applying HOOKS to Shema after Thing model is already created in model file:

export default mongoose.model('Thing', ThingSchema);

Solution

Problem solved when I applied hooks before model created:
In thing.model.js file
./server/api/thing/thing.model.js

'use strict';

import mongoose from 'mongoose';

var ThingSchema = new mongoose.Schema({
  name: String,
  info: String,
  active: Boolean
});

require('./thing.events').applyHooks(ThingSchema);

export default mongoose.model('Thing', ThingSchema);

And thing.events.js was modified to export applyHook function which will apply schema hooks before mongoose model is created.
./server/api/thing/thing.events.js

/**
 * Thing model events
 */

'use strict';

import {EventEmitter} from 'events';
var ThingEvents = new EventEmitter();

// Set max event listeners (0 == unlimited)
ThingEvents.setMaxListeners(0);

// Model events
var events = {
  save: 'save',
  remove: 'remove'
};

function emitEvent(event) {
  return function(doc) {
    ThingEvents.emit(`${event}:${doc._id}`, doc);
    ThingEvents.emit(event, doc);
  };
}

export function applyHooks(schema) {
// Register the event emitter to the model events
  for(var e in events) {
    let event = events[e];
    schema.post(e, emitEvent(event));
  }
}

export default ThingEvents;

@ernistkg
Copy link

ernistkg commented Feb 5, 2017

I might be wrong in reasons but this solution works.

@EliKrumholz
Copy link
Author

Nice work, Ernisto, thanks! I was able to replicate your solution and it fixes the issue on my heroku deployment.

I think you're correct about Thing.schema.post not working properly in ./server/api/thing/thing.events.js but I actually think this is on the end of Mongoose, rolling back the version to "mongoose": "~4.7.9" in package.json seems to fix this error too, which also means it's probably related to #2479, so rolling back mongoose for now and opening an issue with mongoose is probably the best solution.

@ernistkg
Copy link

ernistkg commented Feb 7, 2017

Thank you!

I haven't tried older versions. I thought something was broken in angular-fullstack after refactor maybe or something else.

@Awk34
Copy link
Member

Awk34 commented Feb 7, 2017

Closing as duplicate of #2479

@Awk34 Awk34 closed this as completed Feb 7, 2017
@Awk34 Awk34 added the duplicate label Feb 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants