Events System
This document describes the current PlayServ events API in Unity SDK.
Event definition
Define event payload as class or struct and mark it with [Event]:
[Event(EventType.All)]
[Serializable]
public struct SampleChatEvent
{
public string SenderId;
public string Text;
public long SentAtUnixMs;
}EventType can be Global, Group, User, or All.
Publish events
Global publish:
PlayServ.Publish(new SampleChatEvent
{
SenderId = "player-001",
Text = "Hello from events sample",
SentAtUnixMs = System.DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
});Scoped publish:
PlayServ.PublishForGroup("demo-group", new SampleChatEvent
{
SenderId = "player-001",
Text = "Hello group",
SentAtUnixMs = System.DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
});
PlayServ.PublishForUser("player-002", new SampleChatEvent
{
SenderId = "player-001",
Text = "Hello user",
SentAtUnixMs = System.DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
});Subscribe and unsubscribe
var subscription = PlayServ.Subscribe<SampleChatEvent>(evt =>
{
Debug.Log($"[{evt.SenderId}] {evt.Text}");
});
subscription.Dispose();Observable overload is also available via PlayServ.Subscribe<T>().
Delivery behavior
- Transport-based delivery is available while SDK is connected (
Online). - During reconnect windows (
Reconnecting), delivery may be delayed until session is back online. - In server/in-process mode, delivery can be handled by registered
IEventHandlerimplementation.
Summary
PlayServ events provide typed publish/subscribe messaging with global, group, and user scopes using Publish, PublishForGroup, PublishForUser, and Subscribe.
Last updated on