Search code examples
deepstream.io

How to implement chat on deepstream.io?


I'm trying to implement a real chat using deepstream.io. Online many tutorials do deal with one single chat room that's shared between users, but the problem with this is, if one user deletes a message then its deleted on all users. In normal chat apps, every user has their own inbox, when a user posts a message its duplicated in the sender and receiver's inbox.

So I came up with this.

  1. Use one global listener that listens to all messages being sent, then write those messages to database. Here it means I'm using event listeners. The problem with this method is scaling, because only one server keeps listening and handling all messages, an even bigger problem is if the server listening is down that means the messages don't get persisted.

  2. Use deepstream list and records. This one becomes complicated very fast, I have to use two lists? my list and the recipient list, subscribe to mine for any changes, and when I write a message it means I have to write to two lists the same message. This also means, I have to access two inboxes all on the client side, if the last message is changed I have to update two records as well. So I allow only writing on the recipient list but not delete because that list isn't mine. I don't know about this but can it fail?

Maybe there's something I'm missing. Has anyone successfully implemented a complete chat app with inboxes, and private messaging using deepstream.io?


Solution

  • The delete requirement definitely makes this a bit trickier because you're now talking about mirroring different data sets between users, however there are a few solutions.

    (Using Lists and Records) If your chats are only ever 1 to 1, when a user deletes a message you set a flag on that record indicating the user has deleted it. Then with your valve permissions you don't allow users to see a message if they have deleted it. This will require a bit of application logic, however it's quite a clean approach. Your Valve permissions may look like something as follows

     chat/$userA-$userB:
        read: data.deletedBy !== user.id && (user.id === userA || user.id === userB)
    

    You could also delete the record from the list, however you then end up with the situation you described before.