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:
View → Model
Model → View
Model → Model
View → View
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
Events are queued and handled locally after simulation
Copy
Ask AI
// Model publishes locallythis.publish("effects", "explosion", { x: 100, y: 200 });// View receives only on local devicethis.subscribe("effects", "explosion", this.showExplosion);
Use case: Visual effects, audio cues, local UI updates
Events are executed immediately and synchronously
Copy
Ask AI
// Model publishes to another modelthis.publish("internal", "calculate", { values: [1, 2, 3] });// Another model receives immediatelythis.subscribe("internal", "calculate", this.performCalculation);
Use case: Internal model organization, component communication
Handler executes immediately, before the publish call returns.
Events are queued and handled in the same update cycle
Copy
Ask AI
// View publishes to another viewthis.publish("ui", "toggle-panel", { panel: "settings" });// Another view receives in same update cyclethis.subscribe("ui", "toggle-panel", this.handlePanelToggle);
Use case: UI state management, local interface coordination
class Player extends Multisynq.Model { init() { // Listen to events for this specific player this.subscribe(this.id, "move", this.handleMove); this.subscribe(this.id, "attack", this.handleAttack); }}// Send event to specific playerthis.publish(playerId, "move", { direction: "up" });
// ✅ 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.