View our GitHub

Please visit sails-docs on GitHub to view documentation on your mobile device.

Edit Page

sails.sockets.blast()

Broadcast a message to all sockets connected to the server.

sails.sockets.blast(data);

Or:

  • sails.sockets.blast(eventName, data);
  • sails.sockets.blast(data, socketToOmit);
  • sails.sockets.blast(eventName, data, socketToOmit);

Usage

Argument Type Details
eventName Optional. Defaults to 'message'.
data The data to send in the message.
socketToOmit Optional. If provided, that request socket will not receive the message blasted out to everyone else. Useful when the broadcast-worthy event is triggered by a requesting user who doesn't need to hear about it again.

Example

In a controller action...

sails.sockets.blast('user_logged_in', {
  msg: 'User #' + req.session.userId + ' just logged in.',
  user: {
    id: req.session.userId,
    username: req.session.username
  }
}, req.socket);

Notes

  • The phrase "request socket" here refers to an application-layer WebSocket/Socket.io connection. req.socket also exists for HTTP requests, but it refers to the underlying TCP socket at the transport layer, which is different. Be sure and ensure req.isSocket == true before using req.socket with this method.

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-docs repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.

Reference

We wrote a book!

Get early access to the book
with promotion code: mcneildoc

Get the Book