Title
Create new category
Edit page index title
Edit category
Edit link
Chat Room
In the Chat Room, you can add additional information called options that is automatically synchronized by each participant in the conversation. It is important that the amount of data stored in options is kept minimize to ensure the quickest synchronization possible. You can use options 'tag a room' to change background color, or you can add a latitude as well as longitude.
options consist string key-value pairs
Create or Get 1-on-1 Chat Room with Metadata
The 1-on-1 Chat Room is ideal case when you want a conversation that requires 2 users. You need to set userId to identify the opponent that you want to chat to. If a Chat Room with predefined userId doesn't exist, it will create a new one. Otherwise, if Chat Room with predefined userId already exists, it will return that room and add requester and the opponents as participants. For further information you can see this Chat Room Type.
Below is the example code:
QiscusCore.shared.getRoom(withUser: withUserId,options: options, onSuccess: { (roomModel, _) in print("success")}) { (error) in print("error \(String(describing: error?.message))") }Where:
- withUser: A User identifier that will be used to identify a user and used whenever another user need to chat with this user. It can be anything, whether is is user's email, your user database index, etc. As long as it is unique and a string.
- options: metadata that can be as additional information to Chat Room, which consist key-value, for example key: background, and value: red.
Create Group Chat Room with Metadata
When you want your many users to chat together in a 1-on-1 Chat Room, you need to create Group Chat Room. Basically Group Chat Room has the same concept as 1-on-1 Chat Room, but the different is that Group Chat Room will target array of userIds in a single method.
/// After Create new Group room, you can update room with optionQiscusCore.shared.createGroup(withName: roomName, participants: userIds, onSuccess: { (roomModel) in print("success") //update room QiscusCore.shared.updateRoom(withID: roomModel.id, name: nil, avatarURL: nil, options: options, onSuccess: { (roomModel) in print("success update room with option") }, onError: { (error) in print("error update room \(error)") })}) { (error) in print("error \(error)")}Where:
- roomName: group name
- userIds: list of userId
- avatarUrl: avatar url for group Chat Room
- options: metadata that can be as additional information to Chat Room, which consists of key-value, for example, key: background, and value: red.
You can get created chat room from local data:
if let room = QiscusCore.database.room.find(id: roomId){ //success}Create or Get Channel with Metadata
Creating Channel Chat Room is ideal for a use case that requires a lot of number participants.
You need to set uniqueId to identify a Channel Chat Room. If a Chat Room with predefined uniqueId doesn't exist, it will create a new one with requester as the only one participant. Otherwise, if Chat Room with predefined uniqueId already exists, it will return that room and add requester as a participant.
When the room doesn't exist at the very first call and you do not send avatarUrl and/or roomName, it will use the default value. But, after the second call (room does exist) and you send avatarUrl and/or roomName, it will be updated to that value.
QiscusCore.shared.getRoom(withChannel: channel, options: options, onSuccess: { (room) in print("success")}) { (error) in print("error")}You can get created chat room from local data:
if let room = QiscusCore.database.room.find(id: roomId){ //success}Where :
- uniqueId : identity of a Channel
- options: metadata that can be as additional information to Chat Room, which consists of key-value, for example, key: background, and value: red.
Get Chat Room by Id (Enter Existing Chat Room)
You can enter existing Chat Room by using roomId and create your own chat UI. You can get roomId from RoomModel object that you have created before or participated for 1-on-1, Group Chat or Channel. As a return, you will get a pair of a Chat Room and List of Comments that you can use to init data comment for the first time. As a reference, you can see the details of implementation in this sample.
You can use it to 1-on-1 Chat Room, Group Chat Room, or Channel and here's how to get a Chat Room by roomId:
QiscusCore.shared.getRoom(withID: roomId, onSuccess: { (roomModel,comments) in print("success getting room" + roomModel) print("success getting comments" + comments)}) { (error) in print("error \(String(describing: error?.message))")}Get Chat Room Information
You can get more than one Chat Room, by passing list of roomId, for uniqueIds will deprecate soon, for now you can set same as roomIds. You can see the participant for each room by set showParticipants to true, or you can set false to hide participant data in each room.
QiscusCore.shared.getRooms(withId: arrayRoomID, showParticipant: false, onSuccess: { (rooms) in print("success")}) { (error) in print(error.message)}Where:
withID: array of room idshowParticipant: show participant in room, default is false (optional)
You can get Chat Rooms from your local data, for example:
let rooms = QiscusCore.database.room.all() //success }Get Chat Room List
Get Chat Room list is an ideal case to retrieve all Chat Rooms that Qiscus Account has. This shows up to 100 data per page.
QiscusCore.shared.getAllRoom(limit: 100, page: 1, showEmpty: false, onSuccess: { (rooms, meta) in print("success") }) { (error) in print("error") }Update Chat Room with Metadata
To update your Chat Room metadata, you need roomId, your Chat Room roomName, your Chat Room avatarUrl, and options, for example:
QiscusCore.shared.updateRoom(withID: roomId, name: roomName, avatarURL: avatarURL, options: options, onSuccess: { (rooms) in print("success")}) { (error) in print(error.message)}Where:
- roomId: Chat Room Id
- roomName: room name
- avatarUrl: avatar URL
- options (String:any): metadata that can be as additional information to Chat Room, which consist key-value, for example key: background, and value: red.
Update chat room API works only for Group chat and Channel
Get Participant List In Chat Room
To get participant list in Chat Room, you need roomUniqueId, you can get roomUniqueId within chat room object. By default you get 100 participants, for example:
QiscusCore.shared.getParticipant(roomUniqeId: roomUniqeId, onSuccess: { (participants) in print("success") }) { (error) in print(error.message) }You can get advance by adding some parameter, for example you can order the list based on either ascending (asc) or descending (desc), for example:
QiscusCore.shared.getParticipant(roomUniqeId: roomUniqeId, offset: offset, sorting: .asc, onSuccess: { (participants) in print("success participants=\(participants.count)") }) { (error) in print(error.message) }Where:
- roomUniqueId: unique Id each of Chat Room
- offset: number of offset
- sorting: filtering based on ascending (asc) or descending ( desc), by default refer to asc.
Default return 100 participants for each page.
Add Participant in Chat Room
You can add more than a participant in Chat Room by calling this method addParticipant. You can pass multiple userIds. Once a participant succeed join the Chat Room, they get a new Chat Room in their Chat Room list.
QiscusCore.shared.addParticipant(userEmails: userEmails, roomId: roomID, onSuccess: { (participants) in print("sucess") }) { (error) in print("error =\(error.message)")}Remove Participant in Chat Room
You can remove more than a participant in Chat Room by calling this method removeRoomMember. You can pass multiple userId. Once a participant is removed from the Chat Room, they will not find related Chat Room in their Chat Room list.
QiscusCore.shared.removeParticipant(userEmails: userEmails, roomId: roomID, onSuccess: { (success) in print("sucess") }, onError: { (error) in print("error =\(error.message)") })Get Total Unread Count in Chat Room (Deprecated)
You can get total unread count in every Chat Room. Ideally, this case is used when you want to show badge icon, for example getting total unread count please see the code below:
QiscusCore.shared.unreadCount { (count, error) in if error != nil { print("error") }else{ print("success") } }Qiscus Technology