Sails exposes several methods (sails.sockets.*
) which provide a simple interface for realtime communication with connected socket clients. This is useful for pushing events and data to connected clients in realtime, rather than waiting for them to actively request it using HTTP. This is true regardless of whether a client sockets was connected from a browser tab, from an iOS app, or even from your favorite household IoT appliance.
These methods are implemented using a built-in instance of Socket.io, which is available directly as sails.io
. However, you should almost never use sails.io
directly. Instead, you should call the methods available on sails.sockets.*
. In addition, for certain use cases, you might also want to take advantage of resourceful pubsub methods, a higher level abstraction which are used by Sails built-in blueprint API.
Method | Description |
---|---|
.addRoomMembersToRooms() |
Subscribe all members of a room to one or more additional rooms. |
.blast() |
Broadcast a message to all sockets connected to the server. |
.broadcast() |
Broadcast a message to all sockets in a room. |
.getId() |
Parse the socket ID from an incoming socket request (req ). |
.join() |
Subscribe a socket to a room. |
.leave() |
Unsubscribe a socket from a room. |
.leaveAll() |
Unsubscribe all members of one room from that room and from every other room they are currently subscribed to; except the automatic room with the same name as each socket ID. |
.removeRoomMembersFromRooms() |
Unsubscribe all members of a room from one or more other rooms. |
Don't see a method you're looking for above? A number of
sails.sockets
methods were deprecated in Sails v0.12, either because a more performant alias was already available, or for performance and scalability reasons. Please see the v0.12 migration guide for more information.