By default, you will be subscribed to all the instances returned by the find
blueprint when you do socket.get('/collection')
, so you should be notified when any of them are updated or destroyed. However, you won't get notifications about records being created unless you either a) explicitly call Collection.watch(req)
in your Sails code, or b) set autoWatch
to true in /config/blueprints.js
. See the reference for .watch() and .subscribe() for more info.
The general idea is that for a large-scale app that could have thousands or millions of users, it doesn't make sense to send out notifications to every connected socket whenever a new user is created. Sockets should only be notified about changes to instances they specifically subscribed to.
Update
After finally getting to review the code in question, it looks like there are a few things going on that need attention:
- The socket event being listened for in the code is
Collection
, but it needs to becollection
.Collection
is the model's globalized ID, butcollection
is its identity, which is used as the event name. Case matters here. - Similarly, the
autoWatch
setting in/config/blueprints.js
is case sensitive; you have it asautowatch
. - By default, you don't get socket notifications from the blueprints for events that you yourself trigger. You can use the callback from the socket request methods instead (i.e.
socket.post(url, data, callback)
). This allow you to respond differently to actions that you triggered yourself, rather than ones that were triggered by someone else. If you really want to get your own socket notifications from the blueprints, you can setmirror: true
in/config/blueprints.js
.
It looks like mirror
needs to be added to the docs, although it's mainly a dev tool that you may regret using in production ;)
Once I changed those three things, the code worked fine!