Personalized chat using Sails.js

前端 未结 1 490
我在风中等你
我在风中等你 2021-01-01 08:13

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

1条回答
  •  一向
    一向 (楼主)
    2021-01-01 08:52

    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!

    0 讨论(0)
提交回复
热议问题