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.