0

I am using the @twilio/conversations library in a NextJS + React application. After successfully connecting with an Access Token, I am able to interact with the Client object normally and get participants, conversations, users, etc.

import { Client } from "@twilio/conversations"

const client = new Client(token)

However, when I try to add a participant I am receiving a Forbidden error. The current authenticated user is already a part of the conversation. I am trying to add a different, second user to the conversation. It doesn't matter which conversation or which user I attempt to add as a participant, the error is the same.

const conversation = await client.getConversationByUniqueName(conversationUniqueName)
const res = await conversation.add(userIdentity)

Error response: twilio/conversations forbidden error

Uncaught (in promise) Error: Forbidden
    at Upstream._callee$ (browser.js?5550:2968:1)
    at tryCatch (regeneratorRuntime.js?6f37:86:1)
    at Generator.eval [as _invoke] (regeneratorRuntime.js?6f37:66:1)
    at Generator.eval [as next] (regeneratorRuntime.js?6f37:117:1)
    at asyncGeneratorStep (asyncToGenerator.js?8e4b:3:1)
    at _next (asyncToGenerator.js?8e4b:25:1)
comixninja
  • 748
  • 6
  • 17
  • Are you trying to add the authenticated user to the conversation with the `add` function? Or are you trying to add participants to a conversation that the authenticated user is not a part of? – philnash Sep 09 '22 at 05:42
  • @philnash The current authenticated user is already a part of the conversation. I am trying to add a different, second user to the conversation. – comixninja Sep 09 '22 at 13:25
  • Are you waiting for the [client's `stateChanged` event](http://media.twiliocdn.com/sdk/js/conversations/releases/2.1.0/docs/#instantiating-and-using) before calling those methods? – philnash Sep 11 '22 at 23:58
  • @philnash Yes. I have tried waiting for both `connectionStateChanged` to be `"connected"` and for `stateChanged` to be `"initialized"`. Both connect successfully. But both trigger a **Forbidden** error specifically when I try to use the `conversation.add` function. – comixninja Sep 12 '22 at 14:53
  • @philnash After re-reading the docs for `.add` I'm unclear about the `identity` argument. 1. The description reads `"Add a participant to the conversation by its identity."` What is "its" in this context? 2. The next line down describes the `identity` parameter as the `"Identity of the Client to add."`. These seem to be saying two different things. – comixninja Sep 12 '22 at 14:57
  • As far as I understand, the identity is the text string that is used as the identity of a user when generating them an access token. What are you using as the identity? – philnash Sep 12 '22 at 23:49
  • @philnash There are two users: 1. The authenticated user who is already a Participant of the Conversation and 2. a second user who is not yet added to the Conversation. The authenticated user's identity is used to generate the AccessToken. The AccessToken successfully initializes the client SDK. At that point, the authenticated user is attempting to add the second user to the Conversation: `const res = await conversation.add(secondUserIdentity)` – comixninja Sep 13 '22 at 14:47
  • @philnash My implementation is as described in the [documentation example](https://www.twilio.com/docs/conversations/working-with-conversations#create-your-first-conversation) here under "Add a Participant". Surely the `identity` can be *any* identity? If I only wanted to add the **currently authenticated user** to a Conversation then I would just use `conversation.join()` instead, if I'm not mistaken. – comixninja Sep 13 '22 at 14:52
  • Ooh, further question after checking that the client is fully connected, when you join the room as your user, do you wait for the [`participantJoined`](http://media.twiliocdn.com/sdk/js/conversations/releases/2.1.0/docs/classes/Client.html#participantJoined) event to fire before trying to `add` a user? Are you able to run [this example app](https://github.com/twilio/twilio-conversations-demo-react) successfully with your account credentials? – philnash Sep 14 '22 at 02:07
  • Yes, I have tried waiting until the current user has joined the Conversation with `conversationJoined`. It makes no difference, same error. There is no `participantJoined` event that fires since the current user is already a participant and I have not been able to successfully add the second user with `.add()`—so I don't believe the `participantJoined` event is supposed to fire. – comixninja Sep 14 '22 at 14:44
  • Ah, sorry, I did mean `conversationJoined`. Are you able to run and use the example app I linked to in the comment above? – philnash Sep 14 '22 at 23:09
  • @philnash I am able to use the official demo with my credentials so I don't believe it's an authentication issue. I have not tried the other demo your suggesting since it looks like there's a bunch of setup w/ Firebase and I'm in a mad dash to meet a deadline. But if that will be helpful I will try to set it up this weekend. – comixninja Sep 15 '22 at 19:03
  • @philnash p.s. I had another [question](https://stackoverflow.com/questions/72098884/twilio-conversations-get-online-users) regarding `getSubscribedUsers` that I'm hoping you can take a look at if you have time. – comixninja Sep 15 '22 at 19:04
  • @philnash It's fixed! I updated to the latest versions of `twilio` and `@twilio/conversations` and that did the trick. Thank you so much for your help! – comixninja Sep 24 '22 at 17:24
  • That’s great news! Hope the rest of the app goes well. – philnash Sep 25 '22 at 00:55

0 Answers0