Channel messages

VivoxMessages can be sent into specific Vivox channels that have Text enabled using a ChatCapability of either TextOnly or TextAndAudio.

Send channel messages

You can send a message to a channel by using VivoxService.Instance.SendChannelTextMessageAsync(string channelName, string message) with channelName being the destination channel of the message, and message being the message to be sent.

The following code snippet is an example implementation for sending a message to a set Lobby channel and pulling the text from a MessageInputField InputField object.

public async void SendMessageAsync()
{
    if (string.IsNullOrEmpty(MessageInputField.text))
    {
        return;
    }

    VivoxService.Instance.SendChannelTextMessageAsync(LobbyChannelName, MessageInputField.text);
    MessageInputField.text = string.Empty;
}

Receive channel messages

In order to receive messages from channels with text enabled, the VivoxService.Instance.ChannelMessageReceived Action must be subscribed to.

VivoxMessages sent to a channel are nearly identical to directed messages, with the addition of the ChannelName in VivoxMessage.ChannelName, and with the VivoxMessage.FromSelf field set based on whether the message is from the currently logged in user or not.

The following code snippet is an example line of subscribing to the Action, along with an example of pulling all available information out of the VivoxMessage on receipt.

//Subscribe to this event at some point in the Vivox initialization process,
//preferably before joining any channels.
. . .
VivoxService.Instance.ChannelMessageReceived += OnChannelMessageReceived;
. . .

void OnChannelMessageReceived(VivoxMessage message)
{
    var channelName = message.ChannelName;
    var senderName = message.SenderDisplayName;
    var senderId = message.SenderPlayerId;
    var messageText = message.MessageText;
    var timeReceived = message.ReceivedTime;
    var language = message.Language;
    var fromSelf = message.FromSelf;
    var messageId = message.MessageId;
}

Channel text message history

Vivox allows for users to access the history of a channel's text activity using VivoxService.Instance.GetChannelTextMessageHistoryAsync(string channelName, int requestSize = 10, ChatHistoryQueryOptions chatHistoryQueryOptions = null), with channelName being the name of the channel to request the history of, requestSize being the number of messages to return at a maximum (10 is the default), and chatHistoryQueryOptions being an optional parameter which can do things like specifying a word or phrase contained in the messages, a specific PlayerId sender of the messages, or times to query before or after. More specific information on ChatHistoryQueryOptions can be found on the Chat history query options.

The messages returned in IReadOnlyCollection are listed in order from newest message to oldest message.

The following code snippet is an example of pulling at most recent 25 messages from a set LobbyChannelName, then logging the sender's display name and the message in order from oldest to newest, without using the optional ChatHistoryQueryOptions.

public async void FetchHistoryAsync()
{
    var historyMessages = await VivoxService.Instance.GetChannelTextMessageHistoryAsync(LobbyChannelName, 10);
    //Reversing the messages so they display from oldest to newest
    var reversedMessages = historyMessages.Reverse();
    foreach(VivoxMessage message in reversedMessages)
    {
        Debug.Log($"{message.SenderDisplayName}: {message.MessageText}");
    }
}

Edit channel messages

Vivox allows users to edit the text of messages they have sent into a channel using VivoxService.Instance.EditChannelTextMessageAsync(string channelName, string messageId, string newMessage). channelName being the name of the channel that the message was sent to, the messageId being the id of the message to change, and newMessage being the updated text of the message.

When a message has been successfully edited by anyone in a channel, everyone in the channel will receive a VivoxService.Instance.ChannelMessageEdited Action containing the VivoxMessage that was edited, with the updated MessageText.

public async void UpdateChannelMessageAsync(VivoxMessage messageToUpdate, string updatedMessageText)
{
    await VivoxService.Instance.EditChannelTextMessageAsync(messageToUpdate.ChannelName, messageToUpdate.MessageId, updatedMessageText);
}

Delete channel messages

Vivox allows for users to delete messages they have sent into a channel using VivoxService.Instance.DeleteChannelTextMessageAsync(string channelName, string messageId). channelName being the name of the channel that the message was sent to, and the messageId being the id of the message to delete.

When a message has been successfully deleted by anyone in a channel, everyone in the channel will receive a VivoxService.Instance.ChannelMessageDeleted Action containing the VivoxMessage that was deleted.

public async void DeleteChannelMessageAsync(VivoxMessage messageToDelete)
{
    await VivoxService.Instance.DeleteChannelTextMessageAsync(messageToDelete.ChannelName, messageToDelete.MessageId)
}