0

I'm building a chat app and trying to work out the most efficient way to request multiple conversation threads (both private & group) from a MongoDB database.

My current idea is to loop through the user's contacts on the client side and send a 'getConversation' request to my REST API for each contact. This happens after the user profile data has first been retrieved on the server and sent to the client, in order to populate some of the chat interface as quickly as possible, although I'm not sure if this is optimal given the number of additional requests I'm making (easily 25 - 50 at a time).

I currently think that there are 3 methods I could use:

1.) Send a request to the server for the user data > loop through each contact (private & group) on the server > get each conversation from the DB > send the entire bundle back to the client and separate data into the relevant (Vue / Vuex) modules. Total Requests: 1 / Data Requested: Large

2.) * What I'm doing now: Send an initial request for the user data > receive it on the client > loop through the contacts on the client side > send a separate API request for each contact > populate the conversations as they arrive back on the client. Total Requests: > 20 / Data Requested: Small

3.) Send the initial request for user data > receive it > send a single request for all conversations. I expect this to take longer than option 2, but I could be wrong. Total Requests: 2 / Data Requested: Medium

My objective is to retrieve both user data & conversations as quickly + as efficiently as possible, so I welcome any suggestions or techniques you've used to achieve this kind of thing.

Cheers :)

Notes:

I'm using Vue / Vuex / MongoDB / Express / SocketIO.

jacobedawson
  • 2,929
  • 25
  • 27

1 Answers1

1

TL;DR I'd stay with the second option.

Since you want your app to load as fast as possible and be responsive, you should avoid requesting big chunks of data which you might even end up not using in the app. I'd fetch the first (latest) 5-10 conversations since those would probably be the ones the user would like to read first. Then, if the user wants to read more conversations you haven't fetched from the server yet, you can fetch those (and maybe some conversations from around that time). About your concern regarding sending lots of requests to the server shouldn't be significantly slower than a single big request, and it would make the app much faster and snappier.

For further discussion on this subject check out this question.

Itai Steinherz
  • 777
  • 1
  • 9
  • 19