Edit Page

.broadcast()

Broadcast a message to all sockets in a room (or to a particular socket).

sails.sockets.broadcast(roomNames, data);

Or:

Usage

#
Argument Type Details
1 roomNames String, Array The name of one or more rooms in which to broadcast a message (see sails.sockets.join). To broadcast to individual sockets, use their IDs as room names.
2 eventName String? Optional. The unique name of the event used by the client to identify this message. Defaults to 'message'.
3 data JSON The data to send in the message.
4 socketToOmit req? Optional. If provided, the socket belonging to the specified socket request will not receive the message. This is useful if you trigger the broadcast from a client, but don't want that client to receive the message itself (for example, sending a message to everybody else in a chat room).

Example

#

In an action, service, or arbitrary script on the server:

sails.sockets.broadcast('artsAndEntertainment', { greeting: 'Hola!' });

On the client:

io.socket.on('message', function (data){
  console.log(data.greeting);
});
Additional Examples
#

More examples of sails.sockets.brodcast() usage are available here, including broadcasting to multiple rooms, using a custom event name, and omitting the requesting socket.

Notes

#
  • sails.sockets.broadcast() is more or less equivalent to the functionality of .emit() and .broadcast() in Socket.IO.
  • Every socket is automatically subscribed to a room with its ID as the name, allowing direct messaging to a socket via sails.sockets.broadcast()
  • Be sure to check that req.isSocket === true before passing in req as socketToOmit. For the requesting socket to be omitted, the request (req) must be from a socket request, not just any old HTTP request.
  • data must be JSON-serializable; i.e. it's best to use plain dictionaries/arrays, and make sure your data does not contain any circular references. If you aren't sure, build your broadcast data manually, or call something like rttc.dehydrate(data,true,true) on it first.

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.

Reference

Reference