How do you get the conversation history and participants efficiently?

I am trying to create a "conversation history" page on my app, where you see the "participant's" name and the last message of the conversation (see attached image). I understand the image would have to come from another database.


I have most recently tried the following

  1. Get a list of existing conversations for the logged in user using client.conversations.users.userConversations.list()
  2. Iterate over the number of conversations, and get the last message from each conversation and get participant data
  3. Map over those two datasets and merge the participant data with the last message data

There must be a more efficient way?



Best Answer

  • sbrijmohan
    sbrijmohan admin
    Accepted Answer

    Hi @samunt ,

    This solution should work for you, but may not scale well with users in a large number of channels with lots of message history. Here are some alternatives to consider:

    1. Storing this data themselves. If they utilize callbacks they can create their own DB for the last message sent by each user and query that info to show in their conversation history screen. This is a bit of overhead for them, but should speed up the time to load the conversation history of each participant and reduce API usage: Webhooks
    2. Store the last message from each user as an attribute for that user. With this method, each message sent by the user would also include a call to update the attributes of that user with the message body and timestamp. Rather than querying the userConversations and iterating over each conversation to get the last message from a user, just fetch the user attributes when loading the conversation history page: Attributes

    I hope this helps! ✨

If this is an emergency, please contact Twilio Support. This is not an official Support channel. https://support.twilio.com/
Have an urgent question?
Please contact Twilio Support. This is not an official Support channel.
Contact Support