Edit Page

.publish()

Broadcast an arbitrary message to socket clients subscribed to one or more of this model's records.

Something.publish(ids, data, req);

The event name for this broadcast is the same as the model's identity.

Usage

Argument Type Details
1 ids An array of record ids (primary key values).
2 data The data to broadcast.
3 req Optional. If provided, then the requesting socket will not receive the broadcast.

Example

// On the server:
 tellSecretToBobs: function (req, res) {
 // Get the secret from the request.
 var secret = req.param('secret');
 // Look up all users named "Bob".
 User.find({name: 'bob'}, function(err, bobs) {
 if (err) {return res.serverError(err);}
 // Tell the secret to every client who is subscribed to these users,
 // except for the client that made this request in the first place.
 // Note that the secret is wrapped in a dictionary with a `verb` property -- this is not
 // required, but helpful if you'll also be listening for events from Sails blueprints.
 User.publish(_.pluck(bobs, 'id'), {
 verb: 'published',
 theSecret: secret
 }, req);
 return res.send();
 });
 }
// On the client:
 // Subscribe this client socket to Bob-only secrets
 // > See the `.subscribe()` documentation for more info about subscribing to records:
 // > https://sailsjs.com/documentation/reference/web-sockets/resourceful-pub-sub/subscribe
 io.socket.get('/subscribeToBobSecrets');
 // Whenever a `user` event is received, do something.
 io.socket.on('user', function(msg) {
 if (msg.verb === 'published') {
 console.log('Got a secret only Bobs can hear:', msg.theSecret);
 }
 // else if (msg.verb === 'created') { ... }
 // else if (msg.verb === 'updated') { ... }
 });

Notes

  • Be sure to check that req.isSocket === true before passing in req to refer to the requesting socket. If used, the provided req must be from a socket request, not just any old HTTP request.

Is something missing?

If you notice something we've missed or could be improved on, please follow this link and submit a pull request to the sails repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.

Check out the official Sails VS Code extension

Documentation

Reference

Sails logo

Reference

AltStyle によって変換されたページ (->オリジナル) /