How to handle chat room?

As of now I have a chat room that functions by submitting a message and the message is added to a collection called Chat. It is shown by the client subscribing to the latest 50 messages and showing them in a proper order. I also have a checkbox where you can show messages from your team only. The problem is other teams can send messages and yours will get removed even though you don’t see any new ones on your team. How could I go about changing my system to fit this error?

If I understand your problem correctly (and without seeing your code), I would have two routes (using either Iron Router or Flow Router).

The first route, say ‘/’ for example, will subscribe to all the messages in the Chat collection, and the second route, say ‘/teamMessages’, will subscribe to only the messages that belong to that user’s team.

Then, when the chatbox is activated, the URL is changed to ‘/teamMessages’, and when it is deactivated, the URL is changed to ‘/’.

I hope I understood your problem correctly and I do not anticipate running into your errors with the above implementation.

1 Like

You need to change how you subscribe to the collection, rather than just filtering on the client.

If the user checks the box to only show team chat, then you need to re-subscribe to the collection with that flag set. In your publish function you then can filter on the server, and send 50 messages from the team down the line.

I think what @end is suggesting is more a UI thing, however you don’t need to involve any router for this. Just need to have an event for that checkbox which then updates the subscription.

2 Likes

Thanks for the answer; it sounds like a really good idea. Time to implement it :smile:

Works perfectly and only took a minute to put in! I never knew that you could pass args into a publish through subscribe.

1 Like

Fantastic :smile:

In my app I have been working on it has been very important for my client to only subscribe to the right information.

If you’re note already using it, install Mongol. meteor add msavin:mongol - it will let you see what data has been subscribed to. It will only show you what is being published to the current user at that point in time. So you can easily see if you’re over subscribing to data on your routes.

One step further would be to go all out and get the full meteor toys suite - which lets you autopublish collections. It will let you see both snapshots of your data. Either what has been subscribed - or what it would look like if you over subscribed.