Group Channel
Group Channel allows users to chat privately in small groups.
A group channel is private conversation. Access to the channel requires invitation from already existing group participants. It is an ideal fit for a small group to discuss a topic privately. Also, it is suitable for direct exchange of chat messages between two users.
Creating a Group Channel
You may create a group channel in your iOS app with the help of create() function of our GroupChannel class.
CCPGroupChannel.create(name: CHANNEL_NAME, userIds: PARTICIPANTS_LIST, isDistinct: IS_DISTINCT) {(groupChannel, error) in
if(error == nil){
// New group channel has been created.
}
}
While creating a group channel, extra information like CUSTOM_FILTER can also be added. Use following code for this:
CCPGroupChannel.create(name: CHANNEL_NAME, userIds: PARTICIPANTS_LIST, isDistinct: IS_DISTINCT, custom_filter: CUSTOM_FILTER) {(groupChannel, error) in
if(error == nil){
// New group channel has been created.
}
}
CHANNEL_NAMEis a string and represents the name of your group channel.PARTICIPANTS_LISTis anArray[]of typeStringcontaining the user IDs.IS_DISTINCTis a boolean and determines if a new group channel should be created for the same participants. You can set this boolean totrueto reuse existing channel for the same set of participants.CUSTOM_FILTERis an array which can be used to categorize group channels.
Creating One to One Private Chat
One to One Chat is a special case of Group Channel. If a group channel has 2 participants, then it is a one to one private chat. It is also recommended to use IS_DISTINCT option as true for one to one chat. It will insure that there is always a single channel between 2 users and not multiple.
CCPGroupChannel.create(name: CHANNEL_NAME, userIds: [CURRENT_USER_ID, SECOND_PARTICIPANT_ID], isDistinct: true) {(groupChannel, error) in
if(error == nil){
// New one to one chat has been created.
}
}
CHANNEL_NAMEis a string and represents the name of your group channel.CURRENT_USER_IDis the user id of the current user who is starting one to one chat.SECOND_PARTICIPANT_IDis the user id with which the current user is starting chatting with.
Getting the List of all My Group Channels
To retrieve the list of all group channels of the current user, you would need to use GroupChannelListQuery class. The get() function of GroupChannelListQuery retrieves the group channel list from the server and calls the listener once the result is available.
var groupChannelListQuery: CCPGroupChannelListQuery = CCPGroupChannel.createGroupChannelListQuery()
groupChannelListQuery.setLimit(limit: LIMIT)
groupChannelListQuery.setReference(reference: REFERENCE)
setParticipantState(participantState: PARTICIPANT_STATE_FILTER)
groupChannelListQuery.load(completionHandler: { (channels, erorr) in
if(error == nil){
// Group Channel List received.
}
}
LIMIT- Number of channels to be retrieved. The default is 20.REFERENCE- Reference group channel's ID.PARTICIPANT_STATE_FILTER- Possible values are "ALL", "INVITED", "ACCEPTED"
Getting a Group Channel with ID
You may retrieve a particular group with the help of its ID by calling GroupChannel.get(). This function takes Group Channel ID as argument along with callback listener.
CCPGroupChannel.get(channelId: CHANNEL_ID) {(groupChannel, error) in
if(error == nil){
// Group Channel retrieved successfully.
}
}
Inviting Participants to Group Channel
Already existing participants of the group channel can invite more users to join the channel.
groupChannel.inviteParticipants(PARTICIPANT_IDs, function(error){
if(error == null){
console.log("Participants added successfully")
}
})
PARTICIPANT_IDsis an array of user_ids(string).- Please note that
inviteParticipants()is a function of an instance ofGroupChannelclass.
Accept Invitation to Group Channel
Participants on getting an invitation to join a group channel need to accept the invitation to participate in the channel
groupChannel.acceptInvitation(function(error){
if(error == null){
console.log("Invitation accepted successfully")
}
})
Decline Invitation of Group Channel
Participants on getting an invitation to join a group channel can also decline the invitation.
groupChannel.declineInvitation(function(error){
if(error == null){
console.log("Invitation accepted successfully")
}
})
Leaving Group Channel
A participant of a group channel can leave the channel if she no longer wants to receive messages or notification from the channel.
groupChannel.leave(function(error){
if(error == null){
console.log("channel left successfully")
}
})
Sending Messages in Channel
A participant of a group channel can send two types of messages in it:
TEXT: a normal text messageATTACHMENT: a binary attachment like image, document etc.
Sending Text Message
groupChannel.sendMessage(text: TEXT) {(message, error) in
if(error == nil){
// Message sent successfully
}
}
Addition properties can be sent with a message like MetaData, CustomFilter.
groupChannel.sendMessage(MESSAGE, METADATA, CUSTOM_FILTER, function(error, message){
if(error == null){
console.log("Message sent successfully", message)
}
})
MESSAGE- Text message which needs to be sent.METADATA-Objectwhich can be used to store additional details about a message.CUSTOM_FILTER-Array[]of typeStringwhich can be used to categorize messages.
Sending Attachment
openChannel.sendAttachment(file: FILE, fileName: FILE_NAME, fileType: CONTENT_TYPE, uploadProgressHandler: uploadProgressHandler, completionHandler: { (message, error) in
// Message sent successfully
})
Receiving Messages
In ChatCamp, ChannelListener is used to handle information from server. This information can be a new chat message, new group channel created by some other user etc.
To receive message in a group channel, onGroupChannelMessageReceived ChannelListener is used.
let channelListener = new cc.ChannelListener();
channelListener.onGroupChannelMessageReceived = function(groupChannel, message) {
console.log("Message successfully received in group channel")
}
cc.addChannelListener(LISTENER_ID, channelListener)
Retrieving Previous Messages
Previous messages of a group channel can be retrieved. It can only be retrieved by participants of the group channel.
var previousMessagesQuery: CCPPreviousMessageListQuery
previousMessagesQuery = channel.createPreviousMessageListQuery()
previousMessagesQuery.load(limit: LIMIT) { (messages, error) in
// Previous messages received from server.
}
LIMITis the number of previous messages required to be loaded.REFERENCEis the referencemessage_id. Messages older than thismessage_idwill be loaded. In case to load most recent messages, passREFERENCEasnull.SORT_BYmeans to sort the list by most recently sent and most recently updated. Possible values are "INSERTED_AT" or "UPDATED_AT"
Typing Indicators
Typing indicators can be shown in chat using functions startTyping() and stopTyping(). These are functions of instance of GroupChannel class.
groupChannel.startTyping()
groupChannel.stopTyping()
Receiving Typing Indicators
Typing indicators sent by other users in the group channel can be handled using ChannelListener functionality of ChatCamp SDK. For Typing indicator, onGroupChannelTypingStatusChanged is used. It is similar to how we learnt about Receiving Messages in above sections.
let channelListener = new cc.ChannelListener();
channelListener.onGroupChannelTypingStatusChanged = function(groupChannel) {
console.log("Typing", groupChannel, groupChannel.getTypingParticipants())
}
cc.addChannelListener(LISTENER_ID, channelListener)
Read Receipt
Current user can mark all messages of a group channel as read by calling function markAsRead() of instance of GroupChannel class.
groupChannel.markAsRead()
Getting List of Participants
List of participants of a group channel can be accessed through groupChannel instance of GroupChannel class. participants attribute of groupChannel, is an array of participant objects for this particular groupChannel.
groupChannel.getParticipants()
Updating a message in Group Channel
A participant can update his message in the group channel. A participant is not authorized to update message of other participants.
messageParams = new cc.MessageParams();
messageParams.setText(TEXT);
messageParams.setCustomFilter(CUSTOM_FILTER);
messageParams.setData(DATA);
messageParams.setMetadata(METADATA);
groupChannel.updateMessage(MESSAGE_ID, messageParams, function(error, message){
if(error == null){
console.log("Message Updated Successfully")
}
));
Group Channel Sync
Group Channel details stored locally in groupChannel instance need to be synched with server in case local data becomes stale due to chat disconnect. ChatCamp provides sync() function for this.
CCPGroupChannel.getFromServer(channelId: CHANNEL_ID) {(groupChannel, error) in
if(error == nil){
// Group Channel retrieved successfully.
}
}
Updated less than a minute ago
