twilio / conversations-ios

SPM releases

Home Page:https://www.twilio.com/docs/conversations/ios/changelog

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unable to add a 2nd participant to chat

alanscarpa opened this issue · comments

I'm developing a chat app in Swift. I am following the Quickstart found here: https://www.twilio.com/docs/conversations/ios/exploring-conversations-swift-quickstart#joining-a-conversation

I have 2 devices and I'm trying to get them to join a conversation and chat with eachother. I'm able to successfully get one device into the conversation and sending messagse.

When I launch the app on a 2nd device, and try to join the same conversation, I get an error, but I'm expecting a conversation. Here is the code that returns the error:

// uniqueConversationName = "general"

client.conversation(withSidOrUniqueName: uniqueConversationName) { (result, conversation) in
            // Getting an error that informs me that the participant is not part of the conversation
            if let error = result.error {
                print(error)
            }
            
            // Expected to get a conversation, but conversation is nil
            completion(result, conversation)
        }

The conversation is nil and the error is:
Error Domain=signal.sdk.domain.error Code=50430 "User not participant of conversation" UserInfo={kTCHErrorMsgKey=User not participant of conversation

I know that the user is not a participant of the conversation yet - I want to retrieve the conversation and then using the conversation object, I will join the participant.

With this current state, I can never add a 2nd participant to the conversation.

Why is this error being thrown and how can I fix this?

Here is how the first user creates a conversation and joins it

func createConversation(_ completion: @escaping (Bool, TCHConversation?) -> Void) {
        guard let client = client else {
            return
        }
        // Create the conversation if it hasn't been created yet
        let options: [String: Any] = [
            TCHConversationOptionFriendlyName: uniqueConversationName,
            TCHConversationOptionUniqueName: uniqueConversationName
        ]
        
        client.createConversation(options: options) { (result, conversation) in
            if result.isSuccessful {
                print("Conversation created.")
            } else {
                print(result.error?.localizedDescription ?? "")
                print("Conversation NOT created.")
            }
            completion(result.isSuccessful, conversation)
        }
    }

conversation.addParticipant(byIdentity: currentUserIdentity ?? "", attributes: nil) { result in
                print("Result of conversation join: \(result.resultText ?? "No Result")")
                if result.isSuccessful {
                    self.loadPreviousMessages(conversation)
                }
            }

I want to retrieve the conversation and then using the conversation object, I will join the participant.

It doesn't work this way.
You either add a participant via REST API / webhook or the creator of the conversation may add another participant.

Since conversations are private, other users cannot just add themselves there at will.