Search code examples
node.jssails.jssails.io.js

Multiple separate chats in sails.js


I saw several example of chat functionality on sails.js. However, all of them create one big chat where all users are placed.

How to create multiple chats (so messages posted in one chat isn't visible in another).

I am not sure that I understand the problem correctly. It looks like what happen is that client does io.socket.post('/message', { someMessageData }) and as result get subscribed to all messages.

Is there a way to filter it (on server side) that it got subscribed to messages only in one chat room.

I saw that sockets had id's. And if the server side was using directly socket.io, I would just collect list of id's to which a messages needs to be sent.

However, Sails.js hides this level. And as result, I am not sure how to configure this on the server.

I saw similar question. However, it's reasonably old (taking into account speed of changes in Sails.js). I am not sure where should I put the code regarding Model.subscribe and Model.publish, taking into account that it's all hidden by convention (so it looks like I need to override some default behavior when it subscribe automatically).


Solution

  • First of all, I believe there are three pieces of documentation:

    Frankly, it was hard to put it together only from documentation. So, I looked in the source code.

    Anyway. The problem was that my model has an association with a collection. And as soon as my client read an object of my model, it became subscribed to the model of this collection. And I was seeing ALL changes for this collection model.

    I found that there is a blueprint configuration called "Autowatch". It controls this behavior (whether to subscribe for a model of all associations). It works like a charm If you turn it off in config/blueprints.js.