Subscribe to events

Events are trigger actions when events occur on the Friends notification system. You can subscribe the current user to those events by providing callbacks to invoke when the notifications occur. You also have the ability to subscribe and unsubscribe at any time.

You can subscribe (and unsubscribe) to the following events:

ParameterDescription
RelationshipAddedEvent invoked when a relationship involving the current user gets added.
RelationshipDeletedEvent invoked when a relationship involving the current user gets deleted.
PresenceUpdatedEvent invoked when the presence of a member associated with the current user updates their presence.
MessageReceivedEvent invoked when a user sends a message to the current user.

The following code snippet shows the Events definition.

/// <summary>
/// Event called when a relationship gets added.
/// </summary>
public event Action<IRelationshipAddedEvent> RelationshipAdded;

/// <summary>
/// Event called when a relationship gets deleted.
/// </summary>
public event Action<IRelationshipDeletedEvent> RelationshipDeleted;

/// <summary>
/// Event called when a friend's presence is updated.
/// </summary>
public event Action<IPresenceUpdatedEvent> PresenceUpdated;

/// <summary>
/// Event called when a message is received
/// </summary>
public event Action<IMessageReceivedEvent> MessageReceived;

The following code snippet shows how to subscribe to the RelationshipAdded event.

// Initialize the Friends service
await FriendsService.Instance.InitializeAsync();
FriendsService.Instance.RelationshipAdded += (e) =>
   {
       addedRelationship = e.Relationship;
       Debug.Log("User with ID: " + addedRelationship.Member.Id + " created relationship of type: " + addedRelationship.Type);
   };

The following code snippet shows how to unsubscribe to the RelationshipAdded event.

// Assuming the service has been initialized already
FriendsService.Instance.RelationshipAdded -= MyCallBackFunc