Open Channel

Open Channel enables thousands of users to chat publicly.

An open channel is for public conversation. Any user may join and participate in it without any access restriction. It is perfect fit when you want to have a fixed number of channels for your application.

Creating an Open Channel

An open channel can be created with the help of create() function of OpenChannel class.

CCPOpenChannel.create(name: CHANNEL_NAME) {(openChannel, error) in
	if(error == nil){
  	// New open channel has been created.
  }
}

Getting the list of all open channels

To retrieve the list of existing open channels, you would need to use OpenChannelListQuery class. The get() function of OpenChannelListQuery retrieves the list from ChatCamp backend and the listener is called once the result is available.

var openChannelsQuery: CCPOpenChannelListQuery!
openChannelsQuery = CCPOpenChannel.createOpenChannelListQuery()
openChannelsQuery.load() {(channels, error) in 
	// Open Channel List received.
}

Getting instance of Open Channel based on ID

It is possible to retrieve an instance of open channel if you have the channel ID by using get() function of OpenChannel class.

CCPOpenChannel.get(openChannelId: CHANNEL_ID) {(openChannel, error) in
	if(error == nil){
  	// Get Open channel success
  }
}

Here CHANNEL_ID is the ID of your open channel.

Joining an Open Channel

In order to be able to participate in an open channel, you first need to join it. You may use join() function of OpenChannel class for this.

openChannel.join() {(error) in
	if(error == nil){
		// Join an open channel.
  }
}

Here openChannel is an instance which we created in the earlier section.

📘

If you get disconnected from ChatCamp, then you need to join open channel again in order to continue receiving messages.

Leaving an Open Channel

To stop receiving messages and notifications from an Open Channel, you would need to leave the open channel. You may use leave() function of OpenChannel class for this.

openChannel.leave() {(error) in
	if(error == nil){
		// leave an open channel.
  }
}

Sending Messages in Open Channel

A participant of an open channel can send two types of messages in it:

  • TEXT: a text message
  • ATTACHMENT: a binary attachment like image, document etc.

Sending Text Message

openChannel.sendMessage(text: MESSAGE_TEXT) {(message, error) in
	if(error == nil){
  	// Message sent successfully
  }
}

Sending Attachment

openChannel.sendAttachment(file: FILE, fileName: FILE_NAME, fileType: CONTENT_TYPE, uploadProgressHandler: uploadProgressHandler, completionHandler: { (message, error) in
	// Message sent successfully
})

Here MESSAGE_TEXT is the chat message string and openChannel is the instance of the open channel to which you want to send the message.

Receiving messages from Open Channel

To receive messages, you need to add a channel listener. This listener is called whenever there is any event received from the server.

CCPClient.addChannelDelegate(channelDelegate: self, identifier: UNIQUE_LISTENER_ID)

extension ViewController: CCPChannelDelegate {
   public func channelDidReceiveMessage(channel: CCPBaseChannel, message: CCPMessage) {
       // Chat message received from server.
   }
}

Here UNIQUE_LISTENER_ID is a unique ID to identify your listener. You may add multiple listeners in your app. Ideally there should be one listener per Activity.

Retrieving Previous Messages

Previous messages of an open channel can be retrieved. Participants of the open channel can only retrieve it.

var previousMessagesQuery: CCPPreviousMessageListQuery
previousMessagesQuery = channel.createPreviousMessageListQuery()
previousMessagesQuery.load(limit: LIMIT) { (messages, error) in
	// Previous messages received from server.
}
  • LIMIT is the number of previous messages required to be loaded.