smileham
smileham

Reputation: 1460

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:


My Questions:


What I've Tried:

Upvotes: 16

Views: 1943

Answers (1)

DevAlien
DevAlien

Reputation: 2476

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

Upvotes: 6

Related Questions