You can definitely do peer-to-peer and group chat in Sails using the model-based pubsub methods. It's a lot easier in Sails v0.10 (npm install sails@beta
), and there's a full example at http://github.com/balderdashy/sailsChat. Docs for Sails v0.10 are here.
The trick using v0.10 is to use a combination of subscription contexts and the message event so that only the user themselves are subscribed to their own message
context, but they are subscribed to the update
contexts of everyone they're in a chat room with. That way they only get their own direct messages, but they hear about updates (i.e. name and status changes) to their friends.
To send a direct message to a User
instance in a controller:
User.message(userInstance, msgData);
Any socket client subscribed to the message
context of that instance will receive a user
event with the following data object:
{
id: [the user ID],
verb: "messaged",
data: [the data in msgData]
}
If you want, you can also use low-level Sails Socket methods like join
, leave
, broadcast
and emit
to make a more ad-hoc chat server, but doing it the Sails way lets you use the power of the framework to your advantage!