Question

Is there a way in sails.js or a tutorial that would help me build a peer to peer chat, using sockets, something like pubnub.

I am trying to implement a personalized chat feature using sails.js What that would mean is that I have say multiple users in my application and a user can chat with another user. So if user A sends a message in user B chatbox, B(only) receives and vice-versa.

I have gone through http://sailsjs.org/#!documentation/sockets , the documentation by it gives examples using the models. So I guess it is not for one-to-one chat, but for something like user list and user profile changes kind of stuff.

So I know that I need to use native socket.io code for that. What I need is that the client can open a socket for a particular channel, and then whenever user A types in to user B, that message is stored in the db, as well as written on the socket. I don't know how to get this functionality in Sails.

I can write the code in index event of the MessagesController. So I would need to compute the channel name based on the user A and user B details and write it on the socket for that channel. But I don't know how to write it to the socket for that particular channel. I hope I am clear with what I want. I found this piece of code as an example:

//Code For Server
var io = require("socket.io");
io.sockets.on("connection", function (sock) {
    sock.emit("welcomeMessage", { hello: "world" });
}
io.listen(80);

//Code For Client
var sock = io.connect('http://localhost');
sock.on('welcomeMessage', function (json) {
    //Handle Event Received
});

But it doesn't tell me, how do I write to this particular socket for channel "welcomeMessage" from any controller I want. Something like what PubNub supports (http://www.pubnub.com/) .

Was it helpful?

Solution

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!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top