Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sails.js simple private messaging (pub/sub and models)

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
like image 605
smileham Avatar asked Sep 05 '15 15:09

smileham


1 Answers

The 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.org/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.com/asm-products/boxychat-old

like image 192
DevAlien Avatar answered Oct 12 '22 22:10

DevAlien