Channel messages
Send, receive and manage text messages in Vivox channels.
Read time 3 minutesLast updated 7 months ago
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 with being the destination channel of the message, and being the message to be sent.
VivoxService.Instance.SendChannelTextMessageAsync(string channelName, string message)channelNamemessageThe 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 Action must be subscribed to.
VivoxService.Instance.ChannelMessageReceivedVivoxMessages sent to a channel are nearly identical to directed messages, with the addition of the ChannelName in , and with the field set based on whether the message is from the currently logged in user or not.
VivoxMessage.ChannelNameVivoxMessage.FromSelfThe 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 , with being the name of the channel to request the history of, being the number of messages to return at a maximum (10 is the default), and 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 s can be found in Chat history query options.
VivoxService.Instance.GetChannelTextMessageHistoryAsync(string channelName, int requestSize = 10, ChatHistoryQueryOptions chatHistoryQueryOptions = null)channelNamerequestSizechatHistoryQueryOptionsChatHistoryQueryOptionThe messages returned in are listed in order from newest message to oldest message.
IReadOnlyCollectionThe 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 . 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.
VivoxService.Instance.EditChannelTextMessageAsync(string channelName, string messageId, string newMessage)When a message has been successfully edited by anyone in a channel, everyone in the channel will receive a Action containing the VivoxMessage that was edited, with the updated MessageText.
VivoxService.Instance.ChannelMessageEditedpublic 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 . channelName being the name of the channel that the message was sent to, and the messageId being the id of the message to delete.
VivoxService.Instance.DeleteChannelTextMessageAsync(string channelName, string messageId)When a message has been successfully deleted by anyone in a channel, everyone in the channel will receive a Action containing the VivoxMessage that was deleted.
VivoxService.Instance.ChannelMessageDeletedpublic async void DeleteChannelMessageAsync(VivoxMessage messageToDelete){ await VivoxService.Instance.DeleteChannelTextMessageAsync(messageToDelete.ChannelName, messageToDelete.MessageId)}