Chat using PubSub - Android
For the communication or any kind of messaging in between the participants, VideoSDK provides pubSub
class which use Publish-Subscribe mechanism and can be used to develope wide varitey of functionalities. For example, participants could use it to send chat messages to each other, share files or other media, or even trigger actions like muting or unmuting audio or video.
Now we will see, how we can use PubSub to implement Chat functionality. If you are not familiar with the PubSub mechanism and pubSub
class, you can follow this guide.
Implementing Chat
Group Chat
- First step in creating a group chat is choosing the topic which all the participants will publish and subscribe to send and receive the messages. We will be using
CHAT
as the topic for this one. - On the send button, publish the message that the sender typed in the
EditText
field.
- Kotlin
- Java
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.widget.Toolbar
import live.videosdk.rtc.android.Meeting
import live.videosdk.rtc.android.listeners.PubSubMessageListener
import live.videosdk.rtc.android.model.PubSubPublishOptions
class ChatActivity : AppCompatActivity() {
// Meeting
var meeting: Meeting? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_chat)
/**
* Here, we have created 'MainApplication' class, which extends android.app.Application class.
* It has Meeting property and getter and setter methods of Meeting property.
* In your android manifest, you must declare the class implementing android.app.Application
* (add the android:name=".MainApplication" attribute to the existing application tag):
* In MainActivity.kt, we have set Meeting property.
*
* For Example: (MainActivity.kt)
* var meeting = VideoSDK.initMeeting(context, meetingId, ParticipantName, micEnabled, webcamEnabled, paricipantId, mode, multiStream, customTrack, metaData, signalingBaseUrl,preferredProtocol)
* (this.application as MainApplication).meeting = meeting
*/
// Get Meeting
meeting = (this.application as MainApplication).meeting
findViewById(R.id.btnSend).setOnClickListener(view -> sendMessage());
}
private fun sendMessage() {
// get message from EditText
val message: String = etmessage.getText().toString()
if (!TextUtils.isEmpty(message)) {
val publishOptions = PubSubPublishOptions()
publishOptions.setPersist(true)
// Sending the Message using the publish method
meeting!!.pubSub.publish("CHAT", message, publishOptions)
// Clearing the message input
etmessage.setText("")
} else {
Toast.makeText(
this@ChatActivity, "Please Enter Message",
Toast.LENGTH_SHORT
).show()
}
}
}
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import java.util.List;
import live.videosdk.rtc.android.Meeting;
import live.videosdk.rtc.android.lib.PubSubMessage;
import live.videosdk.rtc.android.listeners.PubSubMessageListener;
import live.videosdk.rtc.android.model.PubSubPublishOptions;
public class ChatActivity extends AppCompatActivity {
// Meeting
Meeting meeting;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
/**
* Here, we have created 'MainApplication' class, which extends android.app.Application class.
* It has Meeting property and getter and setter methods of Meeting property.
* In your android manifest, you must declare the class implementing android.app.Application
* (add the android:name=".MainApplication" attribute to the existing application tag):
* In MainActivity.java, we have set Meeting property.
*
* For Example: (MainActivity.java)
* Meeting meeting = VideoSDK.initMeeting(context, meetingId, ParticipantName, micEnabled, webcamEnabled, participantId, mode, multiStream, customTrack,metaData, signalingBaseUrl,preferredProtocol);
* ((MainApplication) this.getApplication()).setMeeting(meeting);
*/
// Get Meeting
meeting = ((MainApplication) this.getApplication()).getMeeting();
findViewById(R.id.btnSend).setOnClickListener(view -> sendMessage());
}
private void sendMessage()
{
// get message from EditText
String message = etmessage.getText().toString();
if (!message.equals("")) {
PubSubPublishOptions publishOptions = new PubSubPublishOptions();
publishOptions.setPersist(true);
// Sending the Message using the publish method
meeting.pubSub.publish("CHAT", message, publishOptions);
// Clearing the message input
etmessage.setText("");
} else {
Toast.makeText(ChatActivity.this, "Please Enter Message",
Toast.LENGTH_SHORT).show();
}
}
}
- Next step would be to display the messages others send. For this we have to
subscribe
to that topic i.eCHAT
and display all the messages.
- Kotlin
- Java
class ChatActivity : AppCompatActivity() {
// PubSubMessageListener
var pubSubMessageListener =
PubSubMessageListener { message ->
// New message
Toast.makeText(
this@ChatActivity, message.senderName + " says : " + message.message,
Toast.LENGTH_SHORT
).show()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_chat)
//...
// Subscribe for 'CHAT' topic
val pubSubMessageList = meeting!!.pubSub.subscribe("CHAT", pubSubMessageListener)
for (message in pubSubMessageList) {
// Persisted messages
Toast.makeText(
this@ChatActivity, message.senderName + " says : " + message.message,
Toast.LENGTH_SHORT
).show()
}
}
}
public class ChatActivity extends AppCompatActivity {
// PubSubMessageListener
private PubSubMessageListener pubSubMessageListener = new PubSubMessageListener() {
@Override
public void onMessageReceived(PubSubMessage message) {
// New message
Toast.makeText(
ChatActivity.this, message.senderName + " says : "+ message.getMessage(),
Toast.LENGTH_SHORT
).show();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
//..
// Subscribe for 'CHAT' topic
List<PubSubMessage> pubSubMessageList = meeting.pubSub.subscribe("CHAT", pubSubMessageListener);
for(PubSubMessage message : pubSubMessageList){
// Persisted messages
Toast.makeText(
ChatActivity.this, message.senderName + " says : "+ message.getMessage(),
Toast.LENGTH_SHORT
).show();
}
}
}
- Final step in the group chat would be
unsubscribe
to that topic, which you had previously subscribed but no longer needed. Here we areunsubscribe
toCHAT
topic on activity destroy.
- Kotlin
- Java
class ChatActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_chat)
//...
}
override fun onDestroy() {
// Unsubscribe for 'CHAT' topic
meeting!!.pubSub.unsubscribe("CHAT", pubSubMessageListener)
super.onDestroy()
}
}
public class ChatActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
//..
}
@Override
protected void onDestroy() {
// Unsubscribe for 'CHAT' topic
meeting.pubSub.unsubscribe("CHAT", pubSubMessageListener);
super.onDestroy();
}
}
Private Chat
In the above example, if you want to convert into the private chat between two participants, then all you have to do is pass sendOnly
parameter in PubSubPublishOptions
.
- Kotlin
- Java
class ChatActivity : AppCompatActivity() {
//..
private fun sendMessage() {
// get message from EditText
val message: String = etmessage.getText().toString()
if (!TextUtils.isEmpty(message)) {
val publishOptions = PubSubPublishOptions()
publishOptions.setPersist(true)
// Pass the participantId of the participant to whom you want to send the message.
var sendOnly: Array<String> = arrayOf("xyz")
publishOptions.setSendOnly(sendOnly);
// Sending the Message using the publish method
meeting!!.pubSub.publish("CHAT", message, publishOptions)
// Clearing the message input
etmessage.setText("")
} else {
Toast.makeText(
this@ChatActivity, "Please Enter Message",
Toast.LENGTH_SHORT
).show()
}
}
}
public class ChatActivity extends AppCompatActivity {
//...
private void sendMessage()
{
// get message from EditText
String message = etmessage.getText().toString();
if (!message.equals("")) {
PubSubPublishOptions publishOptions = new PubSubPublishOptions();
publishOptions.setPersist(true);
// Pass the participantId of the participant to whom you want to send the message.
String[] sendOnly = {
"xyz"
};
publishOptions.setSendOnly(sendOnly);
// Sending the Message using the publish method
meeting.pubSub.publish("CHAT", message, publishOptions);
// Clearing the message input
etmessage.setText("");
} else {
Toast.makeText(ChatActivity.this, "Please Enter Message",
Toast.LENGTH_SHORT).show();
}
}
}
Downloading Chat Messages
All the messages from the PubSub which where published with persist : true
and can be downloaded as an .csv
file. This file will be available in the VideoSDK dashboard as well as throught the Sessions API.
API Reference
The API references for all the methods and events utilised in this guide are provided below.
Got a Question? Ask us on discord