Joshua
Joshua2mo ago

Understanding clientID values

I want to get the clientID, so I'm checking realtime.clientID within OnRealtimeModelReplaced for my RealtimeComponent. The clientID is -1, does that mean I'm not connected when OnRealtimeModelReplaced is called?
6 Replies
kapa.ai
kapa.ai2mo ago
Hi @Joshua. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To answer your question, let's examine the behavior of clientID and the lifecycle of RealtimeComponents in Normcore. When OnRealtimeModelReplaced is called for a RealtimeComponent, it doesn't necessarily mean that you're fully connected to the room yet. According to the RealtimeComponent lifecycle documentation (https://normcore.io/documentation/realtime/realtimecomponent#using-views-and-components-in-a-scene), the following sequence occurs: 1. Awake() and Start() run, and the RealtimeView registers with Realtime. 2. Realtime creates an empty model for every view and component, setting the model on each and triggering OnRealtimeModelReplaced(). 3. When Realtime actually connects to a room, it either inserts the model into the datastore (if it's new) or replaces the model with server data (if it exists), triggering another OnRealtimeModelReplaced() call. 4. Only after this, Realtime.connected becomes true, and Realtime.didConnectToRoom fires. Given this sequence, it's possible that when OnRealtimeModelReplaced is first called, you're not yet fully connected to the room. This could explain why you're seeing a clientID of -1. To get a valid clientID, you have a few options: 1. Check Realtime.connected before accessing clientID:
if (realtime.connected) {
int clientID = realtime.clientID;
// Use clientID here
}
if (realtime.connected) {
int clientID = realtime.clientID;
// Use clientID here
}
2. Wait for the Realtime.didConnectToRoom event:
realtime.didConnectToRoom += (Realtime realtime) => {
int clientID = realtime.clientID;
// Use clientID here
};
realtime.didConnectToRoom += (Realtime realtime) => {
int clientID = realtime.clientID;
// Use clientID here
};
3. If you specifically need this in OnRealtimeModelReplaced, you might want to implement a coroutine to wait for the connection:
private IEnumerator WaitForConnection() {
while (!realtime.connected) {
yield return null;
}
int clientID = realtime.clientID;
// Use clientID here
}

protected override void OnRealtimeModelReplaced(RealtimeModel previousModel, RealtimeModel currentModel) {
base.OnRealtimeModelReplaced(previousModel, currentModel);
StartCoroutine(WaitForConnection());
}
private IEnumerator WaitForConnection() {
while (!realtime.connected) {
yield return null;
}
int clientID = realtime.clientID;
// Use clientID here
}

protected override void OnRealtimeModelReplaced(RealtimeModel previousModel, RealtimeModel currentModel) {
base.OnRealtimeModelReplaced(previousModel, currentModel);
StartCoroutine(WaitForConnection());
}
It's worth noting that in offline mode, clientID will always be 0, as mentioned in the offline mode documentation (https://normcore.io/documentation/room/offline-mode#things-to-know). By using one of these approaches, you should be able to reliably get the clientID once the connection is established. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@346699113053552640> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Mechabit
Mechabit2mo ago
on model replaced does get called before connecting to a room this is apparently intended behaviour
Joshua
JoshuaOP2mo ago
yeah i forgot >_< this is actually an excellent answer from the bot
Mechabit
Mechabit2mo ago
instead you can listen to the didConnectToRoom event
Joshua
JoshuaOP2mo ago
Yeah I do that elsewhere, I just had duplicate code within ...Replaced so i'll just remove that duplicate, should be fine