Master the event-driven communication system that powers Multisynq Model-View interactions
Events are the primary communication mechanism in Multisynq applications. The publish-subscribe (pub-sub) pattern enables clean, decoupled communication between Models and Views while maintaining perfect synchronization.
Understanding event routing is crucial for proper Multisynq development:
Events are transmitted to the synchronizer and mirrored to all users
Copy
Ask AI
// View publishesthis.publish("game", "player-action", { type: "jump" });// Model receives on ALL devices during next simulationthis.subscribe("game", "player-action", this.handlePlayerAction);
Use case: User input, game actions, any synchronized state changes
// ✅ Good - specific scopethis.publish(playerId, "player-event", data);// ❌ Avoid - broad scope when specific is betterthis.publish("global", "player-event", data);
📦 Small Payloads
Keep event data small and simple
Copy
Ask AI
// ✅ Good - minimal datathis.publish("input", "move", { direction: "left" });// ❌ Avoid - large data structuresthis.publish("input", "move", { player: entirePlayerObject, gameState: entireGameState});
class TemporaryComponent extends Multisynq.View { init() { this.subscribe("temp", "event", this.handler); } destroy() { this.unsubscribe("temp", "event"); // Or use unsubscribeAll() for all subscriptions super.destroy(); }}
Events are the nervous system of Multisynq applications. Understanding when and how to use them effectively is crucial for building responsive, maintainable multiplayer experiences.