Directed messages
Send direct messages between users.
Read time 2 minutesLast updated 7 months ago
Vivox provides directed text by using a similar process to what is used for group text. You can add players to existing text sessions, and you can send text messages directly to any user who is signed in, independent of being in a channel with them.
Use directed text for short user-to-user messages, sometimes called whispers or private/direct messages, or to implement invite to channel capabilities.
Send directed messages
You can send a message to a player by using with playerId being the playerId of the player that the message should be sent to, and message being the message that should be sent.
VivoxService.Instance.SendDirectTextMessageAsync(string playerId, string message)The following code snippet is an example implementation for sending a message to a player passed in from a managed channel roster, with the message being pulled from a dedicated message InputField.
void SendMessageAsync(string playerId){ if (string.IsNullOrEmpty(MessageInputField.text)) { return; } VivoxService.Instance.SendDirectTextMessageAsync(playerId, MessageInputField.text); MessageInputField.text = string.Empty;}
Receive directed messages
In order to receive messages from other users, the event must be subscribed to.
VivoxService.Instance.DirectedMessageReceivedDirected VivoxMessages are nearly identical to channel messages, with the caveat that the will be set to null, and the will be set to false.
VivoxMessage.ChannelNameVivoxMessage.FromSelfThe following code snippet is an example line of subscribing to the event, 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.DirectedMessageReceived += OnDirectedMessageReceived;. . .void OnDirectedMessageReceived(VivoxMessage message){ var senderName = message.SenderDisplayName; var senderId = message.SenderPlayerId; var messageText = message.MessageText; var timeReceived = message.ReceivedTime; var language = message.Language; var messageId = message.MessageId;}
Direct text message history
Vivox also allows for users to access the history of a direct message conversation between two players using . The playerId being the ID of the player to request the history of, requestSize being the number of messages to return at 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, or times to query before or after. More specific information on ChatHistoryQueryOptions can be found on the Chat History query options page.
VivoxService.Instance.GetDirectTextMessageHistoryAsync(string playerId, int requestSize = 10, ChatHistoryQueryOptions chatHistoryQueryOptions = null)The messages returned in are listed in order from newest message to oldest message.
IReadOnlyCollectionThe following code snippet is an example of pulling the 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 .
ChatHistoryQueryOptionsvoid FetchHistoryAsync(){ var historyMessages = await VivoxService.Instance.GetDirectTextMessageHistoryAsync(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}"); }}
Editing direct messages
Vivox allows for users to edit the text of messages they have sent into a channel using with the messageId being the ID of the message to change, and newMessage being the updated text of the message.
VivoxService.Instance.EditDirectTextMessageAsync(string messageId, string newMessage)When a message is successfully edited, logged in players associated with the direct message will receive a Action containing the VivoxMessage that was edited, with the updated MessageText.
VivoxService.Instance.DirectedMessageEditedpublic async void UpdateDirectedMessageAsync(VivoxMessage messageToUpdate, string updatedMessageText){ await VivoxService.Instance.EditDirectTextMessageAsync(messageToUpdate.MessageId, updatedMessageText);}
Deleting direct messages
Vivox allows for users to delete direct messages they have sent using with messageId being the ID of the message to delete.
VivoxService.Instance.DeleteDirectTextMessageAsync(string messageId)When a message is successfully deleted, logged in players associated with the directed message will receive a Action containing the VivoxMessage that was deleted.
VivoxService.Instance.DirectedMessageDeletedpublic async void DeleteDirectMessageAsync(VivoxMessage messageToDelete){ await VivoxService.Instance.DeleteDirectTextMessageAsync(messageToDelete.MessageId)}