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.
OpenChannel.create(CHANNEL_NAME, new OpenChannel.CreateListener() {
@Override
public void onResult(OpenChannel openChannel) {
// Open Channel 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.
OpenChannelListQuery openChannelListQuery = OpenChannel.createOpenChannelListQuery();
openChannelListQuery.get(new OpenChannelListQuery.ResultHandler() {
@Override
public void onResult(List<OpenChannel> openChannelList) {
// 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.
OpenChannel.get(CHANNEL_ID, new OpenChannel.GetListener() {
@Override
public void onResult(OpenChannel openChannel) {
//openChannel is the instance which you may use further.
}
});
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(new OpenChannel.JoinListener() {
@Override
public void onResult() {
// 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.
Sending message to Open Channel
Once you have joined an open channel, you may start sending messages to it. The onSent()
function is fired once the chat message has been delivered successfully to ChatCamp backend and the server has sent the acknowledgement for the same.
openChannel.sendMessage(MESSAGE_TEXT, new OpenChannel.SendMessageListener() {
@Override
public void onSent(Map data) {
// Message has been delivered to the server.
}
});
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.
ChatCamp.addChannelListener(UNIQUE_LISTENER_ID, new ChatCamp.ChannelListener() {
@Override
public void onOpenChannelMessageReceived(Message message) {
// 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
.
Updated less than a minute ago