This page provides a comprehensive reference for the Multisynq Core JavaScript API. Use the table of contents to navigate between major sections like Model
, View
, and Session
.
Core Architecture
Multisynq provides a revolutionary client-side synchronized architecture for building real-time collaborative applications. Unlike traditional server-client architectures, Multisynq runs your application logic on every user’s device in perfect synchronization.
Client-Side Synchronization
Multisynq applications run entirely on the client side. All application logic executes on every user’s device in a deterministic virtual machine, ensuring perfect synchronization without custom server code.
Model-View Separation
Multisynq enforces a strict architectural pattern:
- Model: Contains all application logic and state. Runs in a deterministic VM.
- View: Handles user interface and input. Can read from the model but never writes directly.
- Events: All communication happens through a publish-subscribe event system.
Reflector Network
Simple, stateless message-passing servers handle:
- Event ordering from all clients into a canonical stream
- Synchronized time (heartbeat ticks)
- No application logic execution
Installation & Setup
Installation
Add Multisynq to your HTML page:
<script src="https://cdn.jsdelivr.net/npm/@multisynq/client@latest/bundled/multisynq-client.min.js"></script>
Or import as an ES module:
import * as Multisynq from "https://cdn.jsdelivr.net/npm/@multisynq/client@latest/bundled/multisynq-client.esm.js";
API Key
Get your free API key from multisynq.io/coder.
Multisynq.Model
The Model class provides the foundation for all synchronized objects in Multisynq applications. Models handle computation, maintain state, and ensure that all users see identical behavior through deterministic execution.
Key Concept: Models run identically on every user’s device, ensuring perfect synchronization. They communicate with Views only through publish/subscribe events.
Critical Rules
Implementation Requirements:
- NEVER use
new
constructor - Always use Model.create()
- NEVER override the constructor - Override
init()
method instead
- Always call
Model.register(classId)
for all Model subclasses
- Always call
destroy()
when a model instance is no longer needed
Properties
Unique identifier for the model within the session.
sessionId
Global session identifier shared by all users.
viewCount
Number of users currently in the session.
activeSubscription
Information about the currently executing subscription handler (e.g., { scope, event, source }
).
Instance Methods
init(options, persistentData)
Override this method to initialize your model. Called automatically by create()
.
Configuration options passed to create()
.
Data restored from previous sessions (if using persistence).
publish(scope, event, data)
Send an event to a specific scope.
Event scope (e.g., this.sessionId
).
Data to send with the event.
subscribe(scope, event, handler)
Register an event handler for specific events.
Event scope to listen to.
Method to call when event occurs.
unsubscribe(scope, event, handler)
Remove an event handler.
future(milliseconds)
Schedule a method call for future execution. Returns a proxy object that you call a method on.
this.future(1000).myMethod(arg1, arg2);
Delay in milliseconds before execution.
cancelFuture(method)
Cancel a previously scheduled future message.
The method that was scheduled.
now()
Get the current model time in milliseconds.
random()
Generate a synchronized random number (0-1) identical across all users.
destroy()
Clean up a model instance when it’s no longer needed. This is critical for preventing memory leaks.
getModel(id)
Look up another model in the session by its ID.
Static Methods
Model.create(options)
Create a new model instance. Always use this instead of new
.
Configuration options passed to init()
.
Model.register(classId)
Register a model class with Multisynq. Required for all model subclasses.
Unique identifier for the model class.
Well-Known Models
beWellKnownAs(name)
Make a model instance globally accessible by a string name.
Global name for this model.
wellKnownModel(name)
Access a globally registered model from an instance method.
Name of the well-known model.
Model.wellKnownModel(name)
Access a globally registered model from a static method.
Name of the well-known model.
Multisynq.View
The View class represents the local, user-interface portion of Multisynq applications. Views are local to each device and handle user input, display output, and UI interactions.
Key Concept: Views are local and independent on each device. They subscribe to Model events to stay synchronized and publish events to communicate with Models.
Constructor
new View(model, viewOptions)
Creates a new View instance. Called automatically during Session.join()
.
The model instance this view will display.
Optional configuration passed from Session.join()
.
Properties
viewId
Identifies the current user’s connection to the session.
session
Reference to the session object.
Instance Methods
publish(scope, event, data)
Send an event. For view-to-model communication, data
must be serializable.
subscribe(scope, eventSpec, handler)
Register an event handler.
Event scope to listen to.
Event name or specification object for handling type.
Function to call when event occurs.
Event Handling Types
"queued"
(default): Called once per publish on the next frame.
"oncePerFrame"
: Called only once per frame with the latest data. Ideal for rendering updates.
"immediate"
: Called synchronously during the publish
call.
// Example of 'oncePerFrame' for smooth rendering
this.subscribe(this.model.id, {
event: "positionChanged",
handling: "oncePerFrame"
}, this.updatePosition);
unsubscribe(scope, event, handler)
Remove an event handler.
detach()
Clean up the view when it is no longer needed. This is critical for preventing memory leaks.
now()
Get the current model time in milliseconds.
externalNow()
Get the latest timestamp from the reflector, which may be ahead of now()
if the client is catching up.
Get an extrapolated time based on the last reflector timestamp and local time. Useful for predictive rendering.
update(time)
Called once per frame on the root view (if using "auto"
stepping). Override this for frame-based logic like input polling or animation.
Frame timestamp from requestAnimationFrame
.
wellKnownModel(name)
Access a globally registered model from a view instance.
Name given to Model.beWellKnownAs()
.
Multisynq.Session
The Session class is the main entry point for joining and managing Multisynq sessions.
Session.join(parameters)
The primary method for creating or joining a Multisynq session. It returns a Promise that resolves to a session object.
Key Parameters
Unique application identifier (e.g., “com.example.myapp”).
Session name. Use Multisynq.App.autoSession()
for URL-based names.
Session password. Use Multisynq.App.autoPassword()
for URL-based passwords.
Options passed to your Model’s init()
method.
Ticks per second from the reflector (1-60).
Debug options for console logging.
A full list of parameters can be found in the types.d.ts
file.
Session Object
The object returned from Session.join()
contains:
id
: Unique session identifier.
view
: Your root view instance.
leave()
: A function to leave the session.
step(time)
: A function for manual stepping.
Multisynq.App
A collection of utilities for managing session URLs, QR codes, and debugging tools.
(This section is a placeholder and will be expanded by the automation script.)
Multisynq.Data
The API for storing and retrieving large, immutable data blobs associated with a session.
(This section is a placeholder and will be expanded by the automation script.)
System Events
Your models can subscribe to system-level events published on the sessionId
scope.
view-join
Published when a new user enters the session.
The viewId
of the joining user, or {viewId, viewData}
if viewData
was provided to Session.join()
.
view-exit
Published when a user leaves the session.
The viewId
of the exiting user, or {viewId, viewDta}
.
synced
A view-only event published when the local session has caught up to the reflector and the loading screen is hidden.
true
if in sync, false
if backlogged.