Chat
What's a video call without being able to send messages to each other too? 100ms supports chat for every video/audio room you create.
You can see an example of every type of message (of the types below) being sent and displayed in the advanced sample app.
Addressing messages
Broadcast messages are sent to Everyone in the chat
hmssdk.sendBroadcastMessage
.Direct messages are sent to a specific person
hmssdk.sendDirectMessage
.Group messages are sent to everyone with a particular
HMSRole
. Such as allhosts
or allteachers
or allstudents
hmsSdk.sendGroupMessage
.- Learn more about roles and how to create them on the backend.
Sending Chat Messages
Sending Broadcast Messages
Want to let everyone in the chat know something? Call sendBroadcastMessage
on the instance of HMSSDK
to a send a broadcast.
The parameters are:
- textMessage: The text of the message.
- type: The type of the message, default is
HMSMessageType.CHAT
. - hmsMessageResultListener: An instance of
HMSMessageResultListener
.onSuccess
will be called when the server receives the request. TheonSuccess
contains aHMSMessage
which has the updatedserverReceiveTime
a date containing the server timestamp.onError
will be called with an instane ofHMSException
detailing what went wrong.
💡 Note that the callback only lets you know if the server has received your request for the message or if there was some error.
It does not convey whether the message was delivered to or read by the recipient.
Also it's important to make a new callback per message because it will only contain the results of that particular call for sending a message.
hmssdk.sendBroadcastMessage(textMessage, HMSMessageType.CHAT, object : HMSMessageResultListener { override fun onError(error: HMSException) { } override fun onSuccess(hmsMessage: HMSMessage) { } })
Sending Direct Messages
Got secrets to share? Send a message directly to a single person in the chat with a direct message. Call sendDirectMessage
on an instance of HMSSDK
.
The parameters are:
- textMessage: The text of the message.
- type: The type of the message, default is
HMSMessageType.CHAT
. - peerTo: The
HMSPeer
instance who should receive message. - hmsMessageResultListener: An instance of
HMSMessageResultListener
.onSuccess
will be called when the server receives the request. TheonSuccess
contains aHMSMessage
which has the updatedserverReceiveTime
a date containing the server timestamp.onError
will be called with an instane ofHMSException
detailing what went wrong.
💡 Note that the callback only lets you know if the server has received your request for the message or if there was some error.
It does not convey whether the message was delivered to or read by the recipient.
Also it's important to make a new callback per message because it will only contain the results of that particular call for sending a message.
fun sendDirectMessage(textMessage : String, recipient : HMSPeer) { hmssdk.sendDirectMessage(textMessage, HMSMessageType.CHAT, recipient, object : HMSMessageResultListener { override fun onError(error: HMSException) { } override fun onSuccess(hmsMessage: HMSMessage) { } }) }
Sending Group Messages
Need to call attention to all the hosts? All the teachers? All the developers? Call sendGroupMessage
on an instance of HMSSDK
.
The parameters are:
- textMessage: The text of the message.
- type: The type of the message, default is
HMSMessageType.CHAT
. - hmsRolesTo: The
List<HMSRoles>
of all roles which should receive the message. - hmsMessageResultListener: An instance of
HMSMessageResultListener
.onSuccess
will be called when the server receives the request. TheonSuccess
contains aHMSMessage
which has the updatedserverReceiveTime
a date containing the server timestamp.onError
will be called with an instane ofHMSException
detailing what went wrong.
💡 Note that the callback only lets you know if the server has received your request for the message or if there was some error.
It does not convey whether the message was delivered to or read by the recipient.
Also it's important to make a new callback per message because it will only contain the results of that particular call for sending a message.
private fun sendGroupMessage(textMessage: String, recipients : List<HMSRole> ) { hmssdk.sendGroupMessage(message.message, HMSMessageType.CHAT, recipients, object : HMSMessageResultListener { override fun onError(error: HMSException) { } override fun onSuccess(hmsMessage: HMSMessage) { } }) }
Receiving Chat Messages
When you called hmsSdk.join(config, hmsUpdateListener)
to join a room, the HMSUpdateListener
implementation that was passed in had the callback fun onMessageReceived(message: HMSMessage)
.
This where you'll receive new messages as HMSMessage
during the call. It contains:
data class HMSMessage internal constructor( val message: String, val type: String, val recipient: HMSMessageRecipient = HMSMessageRecipient(), var serverReceiveTime: Date, var sender: HMSPeer )
- message: Content of the text message or the text description of the raw message.
- type: Type of message sent. Default value is
HMSMessageType.CHAT
. - recipient: The intended recipient(s) of this message as a
HMSMessageRecipient
. - serverReceiveTime: timestamp of when the messaging server receives this message. Update the time in your own messages when this comes back from the server in
HMSUpdateListener.onMessageReceived
for accurate ordering of your own messages. - sender: The
HMSPeer
who is sending this message.
Identifying Senders:
The sender of a message is always contained in the sender
field of HMSMessage. This lets you get the name and peer id for any message sender.
Message Body:
The body of the message is in message
as a String.
Time:
The time the message was sent is contained in time
as a Java Date.
Putting together a list of chat messages.
The UI is completely up to you to decide! You'll also need to hold onto all the received messages if you want to display history.
Identifying who the message was for
The HMSMessageRecipient contained in the recipient
field of HMSMessage
lets you know who the message was for.
The HMSMessageRecipient
contains:
class HMSMessageRecipient internal constructor( var recipientPeer: HMSPeer? = null, var recipientRoles: List<HMSRole> = mutableListOf(), var recipientType: HMSMessageRecipientType : HMSMessageRecipientType )
recipientPeer: Only contains a peer when a specific single peer is being direct messaged.
recipientRoles: Only contains values when a group message is being sent to many roles.
recipientType: Will be HMSMessageRecipientType.BROADCAST
for a message being sent to everyone. If this is true, the other two fields will be null and empty respectively.
HMSMessageRecipientType.PEER
will be set when it's a direct message.
HMSMessageRecipientType.ROLES
will be set when it's a message to one or many roles.
A good way to map this to your own app is a class like Recipient
below. You could choose to copy this file into your code for the mapping.
sealed class Recipient { object Everyone : Recipient() { override fun toString(): String = "Everyone" } data class Role(val role : HMSRole) : Recipient() { override fun toString(): String = role.name } data class Peer(val peer : HMSPeer) : Recipient() { override fun toString(): String = peer.name }companion object {fun toRecipient(message : HMSMessageRecipient) : Recipient =when(message.recipientType) {HMSMessageRecipientType.BROADCAST -> EveryoneHMSMessageRecipientType.PEER -> Peer(message.recipientPeer!!)HMSMessageRecipientType.ROLES -> Role(message.recipientRoles.firstOrNull()!!)}} }
So that if you need to convert the message to your own class you can set the recipient like:
override fun onMessageReceived(message: HMSMessage) { ChatMessage( message.sender.name, message.serverReceiveTime, message.message, false,recipient = Recipient.toRecipient(message.recipient)) }