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 a 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.

cc.OpenChannel.create(CHANNEL_NAME, function(error, openChannel) {
 if(!error){
   console.log("Open Channel successfully created", openChannel)
 }
});

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 openChannelListQuery = cc.OpenChannel.createOpenChannelListQuery();
openChannelListQuery.get(function(error, openChannelList){
    if(!error){
        console.log("My Open Channels List Retreived", openChannelList)
    }
})

Getting instance of Open Channel based on ID

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

cc.OpenChannel.get(CHANNEL_ID, function(error, openChannel) {
    if(!error){
    console.log("Open Channel Successfully retrieved", openChannel)
  }
})
  • CHANNEL_ID is the ID of your open channel.

Joining an Open Channel

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.

cc.OpenChannel.get(CHANNEL_ID, function(error, openChannel) {
    openChannel.join(function(error) {
        if(!error){
        console.log("Open Channel Successfully Joined")
    }
  })
})

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.

cc.OpenChannel.get(CHANNEL_ID, function(error, openChannel) {
    openChannel.leave(function(error) {
        if(!error){
        console.log("Open Channel Successfully Left")
    }
  })
})

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(MESSAGE, function(error, message){
    if(!error){
    console.log("Message sent successfully", message)
  }
})

Please note openChannel is the instance of Class OpenChannel.

Sending Attachment

openChannel.sendAttachment(ATTACHMENT, function(progress, total){
    console.log("Tracking progress of upload", progress, total)
}, function(error, message){
  console.log("Attachment sent successfully", message)
})

Receiving Messages

In ChatCamp, ChannelListener is used to handle information from the server. This information can be a new chat message, a new open channel created by some other user etc.

To receive a message in an Open Channel, onOpenChannelMessageReceived ChannelListener is used.

let channelListener = new cc.ChannelListener();
channelListener.onOpenChannelMessageReceived = function(openChannel, message) {
    console.log("New Message received in open channel")
}
cc.addChannelListener(CHANNEL_LISTENER_ID, channelListener);

Retrieving Previous Messages

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

let previousMessageListQuery = openChannel.createPreviousMessageListQuery();

previousMessageListQuery.setLimit(LIMIT);
previousMessageListQuery.setReference(REFERENCE);
previousMessageListQuery.setSortBy(SORT_BY);

previousMessageListQuery.load(function(previousMessageListQueryError, messages) {
    if(!previousMessageListQueryError){
    console.log("Previous messages are successfully loaded")
  }
}
  • LIMIT is the number of previous messages required to be loaded.
  • REFERENCE is the reference message_id. Messages older than this message_id will be loaded. In case to load most recent messages, pass REFERENCE as null.
  • SORT_BY means to sort the list by most recently sent and most recently updated. Possible values are "INSERTED_AT" or "UPDATED_AT"