Skip to main content
Models are the heart of Multisynq applications - they contain all shared state and business logic. To maintain perfect synchronization across all users, models must follow specific constraints and patterns. This guide covers everything you need to know to write robust, synchronized models.

Core Constraints

Critical: Models must follow these constraints to maintain synchronization. Violating these rules will break multi-user functionality.

๐Ÿ”’ Deterministic

Models must produce identical results
  • Same inputs โ†’ Same outputs
  • No randomness (use Multisynq.Random)
  • No external dependencies
  • No system calls

๐Ÿ”„ Serializable

All model state must be saveable
  • No functions in state
  • No DOM references
  • No external objects
  • Pure data structures

Model Registration

Every model class must be registered when defined for proper serialization.
Register simple model classes
The string name must match the class name exactly.

Model Creation and Destruction

Never use new to create model instances. Always use create() and destroy().
Use create() for model instantiation

Initialization with init()

Always use init() for initialization, never constructors. The init() method is called only for new instances, not when restoring from snapshots.
Structure your init() method correctly

Constants and Global Data

No global variables in models. Use Multisynq.Constants for shared constants.
Properly define and use constants
Constants are recursively frozen once the session starts, preventing accidental modification.

Synchronization Rules

Models must be isolated from external systems
Models must be synchronous and deterministic
Donโ€™t create Model โ†’ View โ†’ Model event chains

Advanced: Non-Model Objects

Sometimes you need utility classes that arenโ€™t models. Use the types() system to handle their serialization.
Simple utility classes with default serialization

Best Practices Summary

๐Ÿ—๏ธ Structure

Organize your models properly
  • Register all model classes
  • Use create() and destroy()
  • Initialize in init() method
  • Keep models focused and single-purpose

โšก Performance

Optimize for synchronization
  • Use constants for shared values
  • Batch operations when possible
  • Avoid unnecessary calculations
  • Clean up unused objects

๐Ÿ”’ Safety

Maintain synchronization
  • No external dependencies
  • No async operations
  • No global variables
  • Deterministic behavior only

๐Ÿงช Testing

Test thoroughly
  • Test with multiple users
  • Verify snapshot restoration
  • Check deterministic behavior
  • Test edge cases

Common Mistakes

Avoid these common model development errors:

Next Steps

Writing a Multisynq View

Learn to build views that work with your models

Events & Pub-Sub

Master communication between models and views

Sim Time & Future

Understand timing and scheduling in models

Random

Learn synchronized random number generation
Writing good Multisynq models is fundamental to building successful multiplayer applications. Follow these constraints carefully, and your models will synchronize perfectly across all users, providing a seamless collaborative experience.