ginolin ginolin - 1 month ago 16
Node.js Question

Sails 0.10 ModelIdentity.subscribe or .watch and for publishCreate()?

I'm studying the SailsCasts and I'm working on SailsJS beta version 0.10.

Everything works except when in 0.9.7, I use this:

//subscribe this socket to the User model classroom
User.subscribe(req.socket);

// subscribe this socket to the user instance rooms
User.subscribe(req.socket, users);



  • How to do this for 0.10?



Especially, the subscribe for
publishUpdate
and
publishDestroy
works fine.

For
publishCreate
, I need
'User.subscribe(req.socket)'
and I have in console the warning:

debug: Deprecated: Model.subscribe(socket, null, ...)
debug: (see http://links.sailsjs.org/docs/config/pubsub)
debug: Please use instance rooms instead (or raw sails.sockets.*() methods.)


And then:


  • What are the differences between
    'model.watch()'
    and
    'model.subscribe()'
    ?


Answer

Question 1

I'd prefer to comment (lack rep...), but have you given the docs a good read?

You can't call .subscribe like User.subscribe(req.socket). It requires a second param records. So your User.subscribe(req.socket, users); should work if users is a list of user model instances.

Question 2

I'm no expert (at all...) with node or sails, but the docs #pubsub claimmodel.watch() subscribes clients to publishCreate events for the model instances. I see no mention of publishUpdate,publishDestory, etc. I think it only watches creation events. .subscribe() takes a list of models (or a model) and subscribes the client to publishAdd, publishDestroy, publishRemove, publishUpdate events (by default) for that list of model instances. You can also specify what contexts you want to subscribe to.

So, it seems you actually want to use User.watch(req.socket) instead of .subscribe() if you only want to send the socket publishCreate events. If you need all of them, use something like User.subscribe(req.socket,users,[create,update,destroy,]). If you want to be cool, you can set the autosubscribe property to contain the list of the contexts you care about and just use User.subscribe(req.socket,users) the docs - context.

Cheers

Comments