You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jul 29, 2026. It is now read-only.
Repository navigation
This repository was archived by the owner on Jul 29, 2026. It is now read-only.
Socket IO not syncing "thing" in default app #2491
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?
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 eventsfor(vareinevents){letevent=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:
exportdefaultmongoose.model('Thing',ThingSchema);
Solution
Problem solved when I applied hooks before model created:
In thing.model.js file ./server/api/thing/thing.model.js
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';varThingEvents=newEventEmitter();// Set max event listeners (0 == unlimited)ThingEvents.setMaxListeners(0);// Model eventsvarevents={save: 'save',remove: 'remove'};functionemitEvent(event){returnfunction(doc){ThingEvents.emit(`${event}:${doc._id}`,doc);ThingEvents.emit(event,doc);};}exportfunctionapplyHooks(schema){// Register the event emitter to the model eventsfor(vareinevents){letevent=events[e];schema.post(e,emitEvent(event));}}exportdefaultThingEvents;
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.
Activity
ernistkg commentedon 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.jsAs you can see inside
forloop remove and save events are applied toThingSchema 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:Solution
Problem solved when I applied hooks before model created:
In thing.model.js file
./server/api/thing/thing.model.jsAnd thing.events.js was modified to export
applyHookfunction which will apply schema hooks before mongoose model is created../server/api/thing/thing.events.jsernistkg commentedon Feb 5, 2017
I might be wrong in reasons but this solution works.
EliKrumholz commentedon Feb 5, 2017
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.postnot working properly in./server/api/thing/thing.events.jsbut I actually think this is on the end of Mongoose, rolling back the version to"mongoose": "~4.7.9"inpackage.jsonseems 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 commentedon Feb 7, 2017
Thank you!
I haven't tried older versions. I thought something was broken in
angular-fullstackafter refactor maybe or something else.Awk34 commentedon Feb 7, 2017
Closing as duplicate of #2479