My Goal:
Create a private messaging platform using Sails.js with the simplest code possible
Assumptions of Best Practices:
- Use Sails.js Webockets for realtime notifications
- Use Sails.js PubSub for DB use with websockets
- Use Sails.js .watch() to get messages
My Questions:
- Can I have a socket watch only certain new models (e.g. find where userid matches sender or recipient id) OR do I need to set up rooms? Selective watching seems much easier, but the documentation doesn't seem to support it.
- If any of my above assumptions or questions are not the best way to realize my goal, then what is the simplest way to implement private messaging using Sails?
What I've Tried:
- Subscribing and watching a socket
- Reading the Sails.js documentation
- Looking at the sailsChat example (uses rooms)
- Searching StackOverflow and Google for sails chat examples
My Goal:
Create a private messaging platform using Sails.js with the simplest code possible
Assumptions of Best Practices:
- Use Sails.js Webockets for realtime notifications
- Use Sails.js PubSub for DB use with websockets
- Use Sails.js .watch() to get messages
My Questions:
- Can I have a socket watch only certain new models (e.g. find where userid matches sender or recipient id) OR do I need to set up rooms? Selective watching seems much easier, but the documentation doesn't seem to support it.
- If any of my above assumptions or questions are not the best way to realize my goal, then what is the simplest way to implement private messaging using Sails?
What I've Tried:
- Subscribing and watching a socket
- Reading the Sails.js documentation
- Looking at the sailsChat example (uses rooms)
- Searching StackOverflow and Google for sails chat examples
- 4 Every question should be as clear as this one. – Halim Qarroum Commented Sep 5, 2015 at 15:46
- Are you trying to support only direct chats here? Or can there be rooms with multiple people chatting? – Ryan Quinn Commented Sep 11, 2015 at 2:07
- At first glance, rooms seems to be the simplest solution. There is no selective .watch, unfortunately. – Ryan Quinn Commented Sep 11, 2015 at 2:20
- Yes, it's not clear from your question why you think rooms are not an optimal solution. Assuming you want every connected socket for a specific user to receive messages for that user, then any implementation you e up with will just be a rehashing of rooms anyway. – sgress454 Commented Sep 11, 2015 at 16:13
1 Answer
Reset to default 6 +50The simplest way I guess it is using the socket.io implemented in sails, if I remember correctly it is simply called socket.
All controllers can be called with socket.io (client side) IIRC. The approach that I took is to create a model called messages, and then simply create few endpoints for the messages. If you want to use the models (pub/sub) it is possible to subscribe just to the ones you want. you can subscribe every single user to just a single Model, even if you have plenty of them.
What I used to do is do it manually, when I receive one message, i would emit it to the right client straight away. But if you want to write less code probably you simply have to subscribe the users to your model Model.subscribe()
( http://sailsjs/documentation/reference/web-sockets/resourceful-pub-sub/subscribe )
so when a message is added to the Database then you can send it to whoever you need to.
Here's another example of a chat built on top of sails.js: https://github./asm-products/boxychat-old
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745385911a4625438.html
评论列表(0条)