Best practice advice for loading recent chat for users

Hello,
Recently I developed an Ionic Chat App(my first) after going trough several tutorials. The application realtime chat functionality was developed using socketIO, and the data are saved into MongoDB. Everything works pretty fine.

But here’s the issue. When I select a user to begin chat with, my App loads recent chat data from the server then presents it to the user. In a no network scene user experience can be a bad one. Though I know about native storage but my concern is about consistency of data on both server and mobile. Also I read on PouchDB and CouchDB but I think there’s a better approach to this instead of re-writing my code i.e changing from mongoDB to Poch or CouchDB.

Please kindly advice me of a better way to load my recent chat between two users, instead of fetching from the server each time the user is selected.

Thanks in advance.

1 Like

I’m not sure my way is the best way, but I think it’s a little better than what you describe so here goes.

  1. I separate the work into two providers. More or less, there’s a chat manager provider that a page talks to, and then there’s the app-database provider, which handles communication between app and database. If I need to change backend someday (like going from Mongo to Pouch) I only need to change the second provider. All the commands that the pages execute will remain the same. More or less, all I need to do to change backends is to inject a different daabase provider into the chat manager provider

  2. There’s a local cache of information stored in Local Storage. Something like the last 50 chat messages sent, draw the line wherever you want. That might contain bad information, especially if the user lost internet and the last couple messages never arrived. So I keep track of total number of messages, and if the local number doesn’t match the backend number, I force a resync. Or you could keep a running hash of the data, and if the local hash doesn’t match the backend hash, resync. That’s probably the direction I’m moving, though I don’t have it implemented yet.

2 Likes