Chat

The TSL iOS SDK provides methods for fetching details of a specific current event, enabling you to get chat features.

init(jwtToken:isGuest:showKey:completion:)

Initializes a new instance of the Chat class and confirm the delegate to recieve chat events.

  • Parameters:
    • jwtToken: Generated JWT token
      • Example: eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJzZGtfMmVhMjFkZTE5Y2M4YmM1ZTg2NDBjN2IyMjdmZWYyZjMiLCJleHAiOjE3MTAwMjM0NDEsImp0aSI6InRXc3NBd1Nvb2VoaHp5UTA5NUV1eXk9PSJ9.XtPM3iibdTt-fp8fhm2Gh2T7X0XXuUuIPY17bW648Gk
    • isGuest: A boolean indicating whether the user is a guest user (true) or a federated user (false).
    • showKey: show_key for which you want to subscribe to the channel.
    • completion: (optional)
      • status: A boolean value indicating whether token created successfully or not.
      • error: An optional error that occurred during the token creation process, if any.
let chatInstance = Talkshoplive.Chat(jwtToken: "YourJWTToken", isGuest:true/false, showKey: "YourShowKey")
chatInstance.delegate = someContentViewModel

sendMessage(message:)

Use initialized instance of the Chat class and sends a message using that instance.

  • Parameters:

    • message: The text message to be sent.
  • Completion:

    • status: A boolean value indicating whether the message was sent successfully or not.
    • error: An optional error that occurred during the sending process, if any.
  • Send Message

self.chatInstance.sendMessage(message: newMessage, completion: {status, error in
    if status {
        print("Message Sent!", status)
    } else {
        //If Token is revoked, it will return "PERMISSION_DENIED"
        //If Token is expired, it will return "CHAT_TOKEN_EXPIRED"
        print("Message Sending Failed: \(error.localizedDescription)")
    }
}
  • Recieve New message event listener
class ContentViewModel: ObservableObject, ChatDelegate {
    func onNewMessage(_ message: MessageData) {
        // Handle the received message
        //If it's threaded message, it will have original message details
        if let originalMessage = message.payload?.original?.message {
            print("Original message's sender details", originalMessage.sender)
            print("Original message details", originalMessage.text)
        }
    }
}

deleteMessage(timeToken:)

  • Parameters:

    • timeToken : The time token when message was published.
  • Completion:

    • status: A boolean value indicating whether the message was deleted successfully or not.
    • error: An optional error that occurred during the deletion process, if any.
  • Delete Message

self.chatInstance.deleteMessage(timeToken: timetoken, completion: { status, error in
        if status {
            print("Message Deleted!")
        } else {
            //If Token is revoked, it will return "PERMISSION_DENIED"
            //If Token is expired, it will return "CHAT_TOKEN_EXPIRED"
            print("Message Deletion Failed : “\(error.localizedDescription))
        }
}
  • Recieve Delete message event listener
class ContentViewModel: ObservableObject, ChatDelegate {
    func onDeleteMessage(_ message: Talkshoplive.MessageBase) {
        // Handle the deleted message.
    }
}

getChatMessages(page:includeActions:includeMeta:includeUUID:completion:)

Use to retrieve messages for a specific page, including or excluding actions, metadata, and UUID in the response.

  • Parameters:
    • page: Specifies the page from which to retrieve chat history. If not provided (set to nil), the method will fetch chat history without specifying a particular page.
    • includeActions: Defaults to true. Set to false if you wish to exclude actions from the response.
    • includeMeta: Defaults to true. Set to false if you want to omit metadata from the response.
    • includeUUID: Defaults to true. Set to false if you prefer not to include UUID in the response.
    • completion: A closure invoked upon fetching chat history. It receives a Result enum with an array of MessageBase on success or an Error on failure.
self.chatInstance.getChatMessages(page: page, completion: { result in
    switch result {
    case let .success((messageArray, nextPage)):
        // print("Next Page:", nextPage)
        // print("Received chat messages:", messageArray)
        
        // Access the actions if 'includeActions' is set to true
        for message in messageArray {
            //If it's threaded message, it will have original message details
            if let originalMessage = message.payload?.original?.message {
                print("Original message's sender details", originalMessage.sender)
                print("Original message details", originalMessage.text)
            }
            
            if let actions = message.actions, actions.count > 0 {
                for action in actions {
                    print("Message Action:", action)
                }
            }
        }            
    case .failure(let error):
        // Handle error case
        //If Token is revoked, it will return "PERMISSION_DENIED"
        //If Token is expired, it will return "CHAT_TOKEN_EXPIRED"
        print("Error: \(error.localizedDescription)")
    }
})

clean()

Use to clear all resources associated with the chat instance, including connections and delegates.

self.chatInstance.clean()

updateUser(jwtToken:isGuest:completion:)

Use initialized instance of the Chat class and update use with updated jwtToken

  • Parameters:
    • jwtToken: Updated JWT token
    • isGuest: A boolean indicating whether the user will updated to guest user (true) or a federated user (false).
    • completion:
      • status: A boolean value indicating whether the user was updated successfully or not.
      • error: An optional error that occurred during the sending process, if any.
self.chatInstance.updateUser(jwtToken: "Your Updated JWTToken", isGuest:true/false) { status, error in
    if status {
        print("User Updated successfully!")
    } else {
        print("Error occurred: \(error.localizedDescription)")
    }
}

countMessages(completion:)

Use to retrieve the count of messages using a chat instance.

  • completion:
    • count: An integer value representing the total count of messages.
    • error: An optional error that occurred during the counting process, if any.
self.chat?.countMessages({ count, error in
    if let error = error {
        //If Token is revoked, it will return "PERMISSION_DENIED"
        //If Token is expired, it will return "CHAT_TOKEN_EXPIRED"
        print(error.localizedDescription)
        print("Error fetching messages count: \(error.localizedDescription))"
    } else {
        print("Message Count : \(count)")
    }
})

onStatusChange(error:)

Use to listen event when token is revoked.

func onStatusChange(error: Talkshoplive.APIClientError) {
    
    switch error {
    case .PERMISSION_DENIED:
        //If Token is revoked
        print("Permission Denied")
    case .CHAT_TOKEN_EXPIRED:
        //If Token is expired
        print("Chat token expired")
    case .CHAT_TIMEOUT:
        //Chat timeout
        print("Chat Timeout")
    case .CHAT_CONNECTION_ERROR:
        //connection get dismiss and tried to reconnect and fails
        print("Chat connection error")
    default:
        break
    }
}