Skip to main content
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:
Or import as an ES module:

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

id

string
Unique identifier for the model within the session.

sessionId

string
Global session identifier shared by all users.

viewCount

number
Number of users currently in the session.

activeSubscription

object
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().
object
Configuration options passed to create().
object
Data restored from previous sessions (if using persistence).

publish(scope, event, data)

Send an event to a specific scope.
string
required
Event scope (e.g., this.sessionId).
string
required
Event name.
any
Data to send with the event.

subscribe(scope, event, handler)

Register an event handler for specific events.
string
required
Event scope to listen to.
string
required
Event name to handle.
function
required
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);
number
default:"0"
Delay in milliseconds before execution.

cancelFuture(method)

Cancel a previously scheduled future message.
function
required
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.
string
required
Model ID to look up.

Static Methods

Model.create(options)

Create a new model instance. Always use this instead of new.
object
Configuration options passed to init().

Model.register(classId)

Register a model class with Multisynq. Required for all model subclasses.
string
required
Unique identifier for the model class.

Well-Known Models

beWellKnownAs(name)

Make a model instance globally accessible by a string name.
string
required
Global name for this model.

wellKnownModel(name)

Access a globally registered model from an instance method.
string
required
Name of the well-known model.

Model.wellKnownModel(name)

Access a globally registered model from a static method.
string
required
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().
Model
required
The model instance this view will display.
object
Optional configuration passed from Session.join().

Properties

viewId

string
Identifies the current user’s connection to the session.

session

object
Reference to the session object.

Instance Methods

publish(scope, event, data)

Send an event. For view-to-model communication, data must be serializable.
string
required
Event scope.
string
required
Event name.
any
Event data.

subscribe(scope, eventSpec, handler)

Register an event handler.
string
required
Event scope to listen to.
string | object
required
Event name or specification object for handling type.
function
required
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.

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.

extrapolatedNow()

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.
number
Frame timestamp from requestAnimationFrame.

wellKnownModel(name)

Access a globally registered model from a view instance.
string
required
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

string
required
Your API key.
string
required
Unique application identifier (e.g., “com.example.myapp”).
class
required
Your root Model class.
class
Your root View class.
string
Session name. Use Multisynq.App.autoSession() for URL-based names.
string
Session password. Use Multisynq.App.autoPassword() for URL-based passwords.
object
Options passed to your Model’s init() method.
number
default:"20"
Ticks per second from the reflector (1-60).
string | string[]
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.
string | object
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.
string | object
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.
boolean
true if in sync, false if backlogged.