Skip to main content
Version: 0.1.x

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

  1. 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.
  2. On the send button, publish the message that the sender typed in the EditText field.
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()
}
}
}
  1. Next step would be to display the messages others send. For this we have to subscribe to that topic i.e CHAT and display all the messages.
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()
}
}
}
  1. Final step in the group chat would be unsubscribe to that topic, which you had previously subscribed but no longer needed. Here we are unsubscribe to CHAT topic on activity destroy.
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()
}
}

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.

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()
}
}
}

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