Photon Realtime and Application-Level Retries: Preventing Duplicate Event Execution
0 reputation · 27 Aug 2021, 01:58 UTC
0 reputation · 27 Aug 2021, 01:58 UTC
Photon Realtime utilizes a reliable UDP layer to ensure packets are delivered in order. While the transport layer handles re-transmissions, application-level retry logic is often implemented to ensure critical state changes are acknowledged by the receiver.
A challenge arises when using RaiseEvent with reliable delivery. If a network interruption occurs after the receiver processes the event but before the sender receives the acknowledgment, a manual retry may result in the receiver executing the same game logic multiple times.
While Room Properties offer idempotent updates, RaiseEvent does not natively include a transaction ID to filter duplicate payloads at the OnEvent callback level.
What is the recommended pattern for implementing a lightweight deduplication mechanism within the OnEvent handler to prevent duplicate execution during manual retries? Should a sequence number be integrated into the event data payload, or is there a documented way to track event IDs across the network boundary?
34775 reputation · 27 Aug 2021, 13:44 UTC
To prevent duplicate event execution in Photon Realtime during manual retries, the recommended pattern is to integrate a unique sequence number or transaction ID into the event data payload. Since RaiseEvent does not provide native application-layer deduplication at the OnEvent level, the receiver must track processed IDs manually.
OnEvent handler, check if the incoming `eventId exists in the cache.// Sender-side structure
public class GameEvent {
public long SequenceId;
public string Payload;
}
// Receiver-side OnEvent
void OnEvent(Event code, object[] message) {
var ev = (GameEvent)message[0];
if (processedIds.Contains(ev.SequenceId)) {
return; // Duplicate detected, ignore
}
// 1. Execute critical game logic
ProcessLogic(ev.Payload);
// 2. Track the ID
processedIds.Add(ev.SequenceId);
}
This solution assumes your manual retry logic is triggered by an application-side timeout. If you are strictly using Photon's built-in reliable delivery without manual retry loops, the engine handles re-transmissions internally, and deduplication is only necessary if you are manually re-sending RaiseEvent calls due to perceived failures. I have assumed the frequency of events is low enough that a memory-based cache is feasible.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.