khoi
khoi2w ago

Syncing existing data in a model to a new joiner

When a new person joins and connects to Realtime, I want them to be able to read data from an existing RealtimeDictionary that already has data inside a RealtimeComponent. Where do I do this? In OnRealtimeModelReplaced? If so, how do I access the existing data / when do I know that the local sync has the remote data?
2 Replies
kapa.ai
kapa.ai2w ago
Hi @khoi. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- You're asking about the right approach for syncing existing data in a RealtimeDictionary to new users joining a Normcore session. This is a common scenario when building multiplayer VR applications. Yes, OnRealtimeModelReplaced() is exactly where you should handle this. When a new client joins a room, Normcore automatically synchronizes the existing datastore state to them. The model passed to OnRealtimeModelReplaced() will already contain all the data that exists on the server, including any RealtimeDictionary contents. Here's how you can access the existing data in a RealtimeDictionary:
protected override void OnRealtimeModelReplaced(MyModel previousModel, MyModel currentModel) {
if (previousModel != null) {
// Unregister from events on the previous model
previousModel.myDictionary.modelAdded -= ModelAdded;
previousModel.myDictionary.modelRemoved -= ModelRemoved;
}

if (currentModel != null) {
// Register for events on the new model
currentModel.myDictionary.modelAdded += ModelAdded;
currentModel.myDictionary.modelRemoved += ModelRemoved;

// Process existing data in the dictionary
foreach (KeyValuePair<uint, MyValueModel> pair in currentModel.myDictionary) {
// Process each existing entry in the dictionary
ProcessDictionaryEntry(pair.Key, pair.Value);
}
}
}

private void ModelAdded(RealtimeDictionary<MyValueModel> dictionary, uint key, MyValueModel value) {
// Handle new entries added to the dictionary
ProcessDictionaryEntry(key, value);
}

private void ModelRemoved(RealtimeDictionary<MyValueModel> dictionary, uint key, MyValueModel value) {
// Handle entries removed from the dictionary
}
private void ProcessDictionaryEntry(uint key, MyValueModel value) {
// Process the dictionary entry (create UI, instantiate objects, etc.)
}
protected override void OnRealtimeModelReplaced(MyModel previousModel, MyModel currentModel) {
if (previousModel != null) {
// Unregister from events on the previous model
previousModel.myDictionary.modelAdded -= ModelAdded;
previousModel.myDictionary.modelRemoved -= ModelRemoved;
}

if (currentModel != null) {
// Register for events on the new model
currentModel.myDictionary.modelAdded += ModelAdded;
currentModel.myDictionary.modelRemoved += ModelRemoved;

// Process existing data in the dictionary
foreach (KeyValuePair<uint, MyValueModel> pair in currentModel.myDictionary) {
// Process each existing entry in the dictionary
ProcessDictionaryEntry(pair.Key, pair.Value);
}
}
}

private void ModelAdded(RealtimeDictionary<MyValueModel> dictionary, uint key, MyValueModel value) {
// Handle new entries added to the dictionary
ProcessDictionaryEntry(key, value);
}

private void ModelRemoved(RealtimeDictionary<MyValueModel> dictionary, uint key, MyValueModel value) {
// Handle entries removed from the dictionary
}
private void ProcessDictionaryEntry(uint key, MyValueModel value) {
// Process the dictionary entry (create UI, instantiate objects, etc.)
}
The key point is that by the time OnRealtimeModelReplaced() is called with a non-fresh model, that model already contains all the synchronized data from the server. You don't need to wait for anything else - you can immediately iterate through the dictionary and process its contents. As explained in the Datastore documentation, Normcore handles all the synchronization for you:
When another client makes a change to its datastore, the change is sent to the room server, which will perform validation or transformation of the data before relaying it to all clients.
For more information about collections in Normcore, you can refer to the Collections documentation. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2w ago
<@412870643533479936> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?