Please excuse our look. We're just getting started here.

Want to learn more about Twilio Forums? Check out our FAQ page here.

How can I retrieve an existing conversation?

I am doing a webapp with nextjs and I need to integrate twilio conversations.

I am starting with a very basic feature: sending a message to a conversation. Basically I need to send a message to another user from the app. First of all I have to say there are two ways to send a message from my webapp: from the chat section where I can send message only to the already created conversations and from the user's profile where I can send only one message from a dedicated modal and eventually create one conversation if it is the first time I send a message to him.

The first thing I want to face now is to send a single message to a conversation from the user's profile. I did the following steps:

1) I created an API which creates or gets (if it is already created) a conversation and returns to the client the conversation's unique name and the user's token. This is the response

  "twilioToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImN0eSI6InR3xxxxxxxxxxxxxx",
  "convoUniqueName": "bc688e7e-640a-42dd-bfcb-5094e7c11b0d"

2) I did a method which takes the token, the conversation's unique name and the message and sends it.

import { Client, State } from '@twilio/conversations';
import toast from 'react-hot-toast';

const sendMessageToConversation = async (
  token: string,
  uniqueName: string,
  message: string
) => {
  const client = await new Client(token);
  client.on('stateChanged', async (state: State) => {
    console.log('client', client);
    if (state === 'initialized') {
      try {
        const conversation = await client.getConversationByUniqueName(uniqueName);
        await conversation.join();
        if (message && String(message).trim()) {
          await conversation.sendMessage(message);
      } catch {
        toast.error('Unable to create conversation, please reload this page');

So what is the problem? The problem is that it doesn't retrieve the conversation (await client.getConversationByUniqueName(room)). Same thing by doing client.getConversationBySid(convoSid)

The console.log(client) logs the client and seems to be ok.

If I put out the line code where I get the conversation outside of try-catch block I have the following error:

So how can I get the conversation by its unique name or sid client side? I am using react with typescript.

If this is an emergency, please contact Twilio Support. This is not an official Support channel.
Have an urgent question?
Please contact Twilio Support. This is not an official Support channel.
Contact Support