# Message Events

## Inherit from EasyManager.cs

```csharp
using EasyCodeForVivox;

public class VivoxManager : EasyManager
{

}
```

## Inject EasyEvents

```csharp
using EasyCodeForVivox.Events;
using UnityEngine;
using Zenject;

public class VivoxEvents : MonoBehaviour
{
    EasyEvents _events;

    [Inject]
    private void Initialize(EasyEvents events)
    {
        _events = events;
    }
}
```

## Dynamic Events

#### Make sure Dynamic Events are enabled

<figure><img src="https://3152232848-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MWHF2U_tfZEUaf_IsL-%2Fuploads%2FgNWd8mgjCB90w9n3fR8A%2FEnableDynamicEvents.png?alt=media&#x26;token=f9e1f6a3-8d21-49a1-9ff5-d1a8151efad1" alt=""><figcaption></figcaption></figure>

## How to Subscribe to Message Events in EasyCode&#x20;

#### EasyManager

{% hint style="info" %}
Keeping the *<mark style="color:blue;">**base**</mark>* methods are not necessary. They are simply <mark style="color:blue;">**Debug.Logs()**</mark>. Feel free to delete them
{% endhint %}

<pre class="language-csharp"><code class="lang-csharp"><strong>// Message Event Callbacks
</strong>
protected override void OnChannelMessageRecieved(IChannelTextMessage textMessage)
{
    base.OnChannelMessageRecieved(textMessage);
}

protected override void OnDirectMessageRecieved(IDirectedTextMessage directedTextMessage)
{
    base.OnDirectMessageRecieved(directedTextMessage);
}

protected override void OnDirectMessageFailed(IFailedDirectedTextMessage failedMessage)
{
    base.OnDirectMessageFailed(failedMessage);
}
</code></pre>

#### EasyEvents

```csharp
public void SubscribeToMessageEvents()
{
    _events.ChannelMessageRecieved += OnChannelMessageRecieved;

    _events.DirectMessageRecieved += OnDirectMessageRecieved;
    _events.DirectMessageFailed += OnDirectMessageFailed;
}

public void UnsubscribeToMessageEvents()
{
    _events.ChannelMessageRecieved -= OnChannelMessageRecieved;

    _events.DirectMessageRecieved -= OnDirectMessageRecieved;
    _events.DirectMessageFailed -= OnDirectMessageFailed;
}



protected virtual void OnChannelMessageRecieved(IChannelTextMessage textMessage)
{
    Debug.Log($"From {textMessage.Sender.DisplayName} : {textMessage.ReceivedTime} : {textMessage.Message}");
}

// This feature is expiramental and may be removed later
protected virtual void OnEventMessageRecieved(IChannelTextMessage textMessage)
{
    Debug.Log($"Event Message From {textMessage.Sender.DisplayName} : {textMessage.ReceivedTime} : {textMessage.ApplicationStanzaNamespace} : {textMessage.ApplicationStanzaBody} : {textMessage.Message}");
}

protected virtual void OnDirectMessageRecieved(IDirectedTextMessage directedTextMessage)
{
    Debug.Log($"Recived Message From : {directedTextMessage.Sender.DisplayName} : {directedTextMessage.ReceivedTime} : {directedTextMessage.Message}");
}

protected virtual void OnDirectMessageFailed(IFailedDirectedTextMessage failedMessage)
{
    Debug.Log($"Failed To Send Message From : {failedMessage.Sender}");
}
```

#### Dynamic Events

Make sure the parameter in your method matches the event type. See what parameter is required for each [Message Event here](https://fullstackindie.gitbook.io/easy-code-for-vivox/api-info/easyevents.cs#message-events). Dynamic events will dynamically invoke your method at runtime on every game object that has a dynamic event attribute. Because of this **there is no need to Subscribe/Unsubscribe from events** with the usual <mark style="color:red;">**+=/-=**</mark>

```csharp
[ChannelMessageEvent(ChannelMessageStatus.ChannelMessageRecieved)]
private void OnChannelMessageRecieved(IChannelTextMessage textMessage)
{
    Debug.Log($"From {textMessage.Sender.DisplayName} : {textMessage.ReceivedTime} : {textMessage.Message}");
}

// This feature is expiremental and may be removed later
[ChannelMessageEvent(ChannelMessageStatus.EventMessageRecieved)]
private void OnEventMessageRecieved(IChannelTextMessage textMessage)
{
    Debug.Log($"Event Message From {textMessage.Sender.DisplayName} : {textMessage.ReceivedTime} : {textMessage.ApplicationStanzaNamespace} : {textMessage.ApplicationStanzaBody} : {textMessage.Message}");
}

[DirectMessageEvent(DirectMessageStatus.DirectMessageRecieved)]
private void OnDirectMessageRecieved(IDirectedTextMessage directedTextMessage)
{
    Debug.Log($"Recived Message From : {directedTextMessage.Sender.DisplayName} : {directedTextMessage.ReceivedTime} : {directedTextMessage.Message}");
}

[DirectMessageEvent(DirectMessageStatus.DirectMessageFailed)]
private void OnDirectMessageFailed(IFailedDirectedTextMessage failedMessage)
{
    Debug.Log($"Failed To Send Message From : {failedMessage.Sender}");
}
```

#### Dynamic Async Events

Make sure the parameter in your method matches the event type. See what parameter is required for each [Message Event here](https://fullstackindie.gitbook.io/easy-code-for-vivox/api-info/easyevents.cs#message-events).  Dynamic events will dynamically invoke your method at runtime on every game object that has a dynamic event attribute. Because of this **there is no need to Subscribe/Unsubscribe from events** with the usual <mark style="color:red;">**+=/-=**</mark>

{% hint style="info" %}
**Remember to use&#x20;**<mark style="color:blue;">**async void**</mark>**&#x20;or&#x20;**<mark style="color:blue;">**async Task**</mark>**&#x20;or else the event may run synchronously**
{% endhint %}

{% hint style="warning" %}
**More information on the methods called in any async method can be found here.** [**Unity Gaming Services Examples**](https://fullstackindie.gitbook.io/easy-code-for-vivox/related-info/unity-gaming-services)**. They are direct copies from Unity's docs.&#x20;**<mark style="color:red;">**These are just examples and don't mimic real world use cases**</mark>
{% endhint %}

```csharp
[ChannelMessageEventAsync(ChannelMessageStatus.ChannelMessageRecieved)]
private async void OnChannelMessageRecievedAsync(IChannelTextMessage textMessage)
{
    Debug.Log($"From {textMessage.Sender.DisplayName} : {textMessage.ReceivedTime} : {textMessage.Message}");
    await SavePlayerData();
}

// This feature is expiremental and may be removed later
[ChannelMessageEventAsync(ChannelMessageStatus.EventMessageRecieved)]
private async void OnEventMessageRecievedAsync(IChannelTextMessage textMessage)
{
    Debug.Log($"Event Message From {textMessage.Sender.DisplayName} : {textMessage.ReceivedTime} : {textMessage.ApplicationStanzaNamespace} : {textMessage.ApplicationStanzaBody} : {textMessage.Message}");
    await SavePlayerData();
}

[DirectMessageEventAsync(DirectMessageStatus.DirectMessageRecieved)]
private async void OnDirectMessageRecievedAsync(IDirectedTextMessage directedTextMessage)
{
    Debug.Log($"Recived Message From : {directedTextMessage.Sender.DisplayName} : {directedTextMessage.ReceivedTime} : {directedTextMessage.Message}");
    await SavePlayerData();
}

[DirectMessageEventAsync(DirectMessageStatus.DirectMessageFailed)]
private async void OnDirectMessageFailedAsync(IFailedDirectedTextMessage failedMessage)
{
    Debug.Log($"Failed To Send Message From : {failedMessage.Sender}");
    await SavePlayerData();
}
```
