sails.sockets.blast()

Broadcast a message to all sockets connected to the server.

  1. 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
1 eventName ((string)) Optional. Defaults to 'message'.
2 data ((*)) The data to send in the message.
3 socketToOmit ((Socket)) 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…

  1. sails.sockets.blast('user_logged_in', {
  2. msg: 'User #' + req.session.userId + ' just logged in.',
  3. user: {
  4. id: req.session.userId,
  5. username: req.session.username
  6. }
  7. }, 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.