Enroll the requesting client socket in the "class room" for this model, causing it to receive broadcasts every time publishCreate()
is called on this model. In addition, this client socket will be subscribed every new record it hears about automatically.
Something.watch(req);
Argument | Type | Details | |
---|---|---|---|
1 | req |
The incoming socket request (req ). |
Also see Notes below for an important reminder about security.
User.find({
limit: req.param('limit'),
skip: req.param('skip'),
sort: 'name ASC'
}).exec(function(err, users) {
if (err) return res.serverError(err);
if (req.isSocket) {
// If this code is running, it's made it past the `isAdmin` policy, so we can safely
// watch for `.publishCreate()` calls about this model and inform this socket, since we're
// confident it belongs to a logged-in administrator.
User.watch( req );
}
// If this is request wants JSON (i.e. AJAX), then send a JSON response.
if (req.wantsJSON) {
return res.json(users);
}
// Otherwise serve an HTML page.
return res.view('admin/user-dashboard', {
users: users
});
});
- Much like the default blueprint API,
.watch()
should be used with care. Client sockets allowed to.watch()
will receive broadcasted messages every timepublishCreate()
is called, and then be subscribed to future notifications (likepublishUpdate()
) about those new records. This method is a great fit for use cases where access control is "all or nothing"-- e.g. something like an admin dashboard, or a publicly-available endpoint.- Be sure and check
req.isSocket === true
before passing inreq
to refer to the requesting socket. The providedreq
must be from a socket request, not just any old HTTP request.watch()
will only work when the request is made over a socket connection (e.g. usingio.socket.get()
), not over HTTP (e.g. usingjQuery.get()
).