Message metadata
Attach metadata to Vivox messages for additional context.
Read time 1 minuteLast updated 2 days ago
Send and receive additional information between players by attaching metadata to Vivox messages. This metadata can enhance player communication and support gameplay features.. Send information such as display names, a player’s local mute state, game data and more by adding metadata to messages. The metadata doesn't alter the original message content but provides context or instructions to the recipient or the game logic. Attach the metadata by using
MessageOptions.MetadataVivoxMessage.Metadata
The following code sample is an example of sending a message with JSON metadata that communicates the user’s local mute state:
The following code sample is an example of parsing a message with JSON metadata:using UnityEngine;using Unity.Services.Vivox;[System.Serializable]public class MetadataInfo{ public string Type;}[System.Serializable]public class PlayerMuteInfoMetadata{ public string Type = "PlayerMuteInfo"; public string PlayerId; public bool LocallyMuted;}public async void MutePlayerLocally(VivoxParticipant participant){ // ... (player mute logic) // If it was a self-mute, the local user's input device would be muted. if (participant.IsSelf) { // Send a message with metadata to the channel communicating the local player's updated input device mute state to other participants. var messageOptions = new MessageOptions { Metadata = JsonUtility.ToJson(new PlayerMuteInfoMetadata { PlayerId = VivoxService.Instance.SignedInPlayerId, LocallyMuted = VivoxService.Instance.IsInputDeviceMuted }) }; await VivoxService.Instance.SendChannelTextMessageAsync(participant.ChannelName, "Metadata", messageOptions); }}
VivoxService.Instance.ChannelMessageReceived += OnChannelMessageReceived;// In your channel message received handler:void OnChannelMessageReceived(VivoxMessage message){ // You can now parse or use the metadata that was attached when sending the message. if (!string.IsNullOrEmpty(message.Metadata)) { // Parse JSON metadata. var metadataType = JsonUtility.FromJson<MetadataInfo>(message.Metadata).Type; switch (metadataType) { case "PlayerMuteInfo": var playerInfo = JsonUtility.FromJson<PlayerMuteInfoMetadata>(message.Metadata); // Handle updating player info in UI. break; default: break; } }}