I have a simple login script. I created a method in the SessionController called create to handle logins. I do all the needed validation and then set the user as active and save them. I also use User.publishUpdate()
to send the message back to the client.
The problem is that I recieve no messages at all. When the page is loaded I call User.subscribe(req.socket, users);
for all users so they are all subscribed. Not really sure why the messages aren't sent.
I am using sails 0.10.0.
This is the subscribe method which is called on page load:
User.find({}).exec(function (error, users) {
if (error) {
res.json({
error: error
});
}
// Subscribe to the model class
User.subscribe(req.socket);
// subscribe to the model instance
User.subscribe(req.socket, users);
res.send(200);
});
And the create method for logins (removed code that doesn't pertain to this question):
user.active = true;
req.session.authenticated = true;
req.session.User = user;
user.save(function (error, user) {
if (error) {
res.send(error, 500);
}
User.publishUpdate(user.id, {
loggedIn: true,
id: user.id,
action: ' has logged in'
});
res.json(user, 200);
});
I am sending the request via sockets to the subscribe and login methods
socket.get('/user/subscribe');
socket.get('/session/create', {//stuff}, callback)
PubSub is something that has changed quite a bit with Sails v0.10, and it looks like you're using some v0.9.x semantics. You can read all about it in the migration guide and the new Model Methods reference, but as far as what impacts you, the main thing is:
Messages are no longer published using the "message" event. Instead, they are published using the model's identity as the event name. So, change your listener to:
and you'll start hearing something.
The other thing that pertains to you is that the "Model classrooom" is deprecated, as is using
.subscribe()
with one argument to subscribe to it. Instead, useUser.watch(req)
to get notifications about newly created Users.Update along with the
.watch()
method, there is also now anautoWatch
option available insails.config.blueprints
(set in yourconfig/blueprints.js
file) that, iftrue
, will revert to v0.9.x behavior: any time a socket is subscribed to a model instance, it will also start "watching" that model class for "create" events.