This is the core construct for dealing with distributed data in Convergence. It is essentially a distributed data model: Anybody in the same domain with permissions to this model can open it at the same time, be notified of remote changes, and modify the data within, which is itself synchronized in the same way between all participants. Any co-modification conflicts are resolved automatically and consistently for all connected users so that everybody sees the same thing.

See this page in the developer guide for a few of the interesting things you can do with a RealTimeModel.

Any data that can be represented with JSON can be stored in a RealTimeModel. This allows a huge range of applications to sync data instantly and seamlessly with Convergence.

To work with the data within (reading and writing):

- root to get the root object
- elementAt to query for a particular node within the data

See RealTimeModelEvents for the events that may be emitted on this model.

### Hierarchy

- ConvergenceEventEmitter<IConvergenceEvent>
  - RealTimeModel

### Implements

- ObservableModel

## Index

### Properties

- Events

### Methods

- addListener
- batchSize
- cancelBatch
- close
- collaborators
- collaboratorsAsObservable
- collectionId
- completeBatch
- createdTime
- elementAt
- elementReference
- emitLocalEvents
- endBatch
- events
- isBatchStarted
- isClosing
- isCommitted
- isLocal
- isOpen
- isSubscribedOffline
- maxTime
- maxVersion
- minTime
- minVersion
- modelId
- off
- on
- once
- permissions
- permissionsManager
- reference
- references
- removeAllListeners
- removeListener
- removeListeners
- resynchronizingCollaborators
- root
- session
- startBatch
- subscribeOffline
- time
- unsubscribeOffline
- version
- whenClosed

## Properties

### StaticEvents

Events: RealTimeModelEvents = RealTimeModelEventConstants

A mapping of the events this model could emit to each event's unique name. Use this to refer an event name:

    rtModel.on(RealTimeModel.Events.COLLABORATOR_OPENED, function listener(e) {
      // ...
    })

## Methods

### addListener

- addListener(event: string, listener: ConvergenceEventListener<IConvergenceEvent>): ConvergenceEventEmitter<IConvergenceEvent>

- Inherited from ConvergenceEventEmitter.addListener

  Adds a new event listener for the specified event. The class will ignore duplicate registrations of the same listener to the same event.

  #### Parameters

  - ##### event: string

    The name of the event to add the listener for.

  - ##### listener: ConvergenceEventListener<IConvergenceEvent>

    The listener callback to register.

  #### Returns ConvergenceEventEmitter<IConvergenceEvent>

  This object, in support of a fluent API.

### batchSize

- batchSize(): number

- Returns the number of model changes that have accumulated since startBatch was called.

  #### Returns number

### cancelBatch

- cancelBatch(): void

- Cancels the batch job. Note that calling this once changes have occurred on this model since the batch was started (that is, `batchSize() > 0`) will result in an error.

  #### Returns void

### close

- close(): Promise<void>

- Closes the model, emitting the appropriate events to any other participants and cleaning up any opened resources.

  #### Returns Promise<void>

### collaborators

- collaborators(): ModelCollaborator[]

- Returns an array of collaborators, or other users who currently have this model open.

  #### Returns ModelCollaborator[]

  an array of collaborators

### collaboratorsAsObservable

- collaboratorsAsObservable(): Observable<ModelCollaborator[]>

- Returns an observable which emits whenever a collaborator opens or closes this model. This is useful for keeping an up-to-date listing of current collaborators.

  Also see the PresenceService for a more robust mechanism for keeping tabs on who is available for participation within the domain.

  #### Returns Observable<ModelCollaborator[]>

### collectionId

- collectionId(): string

- Implementation of ObservableModel.collectionId

  The collection ID to which this model belongs.

  #### Returns string

### completeBatch

- completeBatch(): void

- Sends any accumulated model changes to the server as one atomic operation. This also removes the batch flag such that subsequent individual changes are immediately processed and sent as normal.

  #### Returns void

### createdTime

- createdTime(): Date

- Implementation of ObservableModel.createdTime

  The timestamp at which this model was first created.

  #### Returns Date

### elementAt

- elementAt(path: Path): RealTimeElement<any>
- elementAt(...path: PathElement[]): RealTimeElement<any>

- Given a search path, returns the RealTimeElement at that path, or null if no such element exists.

  #### Parameters

  - ##### path: Path

    the search path for accessing a node within this model's data

  #### Returns RealTimeElement<any>

- Given an array of search path elements, returns the RealTimeElement at that path, or null if no such element exists.

  #### Parameters

  - ##### Rest ...path: PathElement[]

    an array of search path elements (which in totality are a Path)

  #### Returns RealTimeElement<any>

### elementReference

- elementReference(key: string): LocalElementReference

- Creates an Element Reference, which is a Reference bound to one more elements in this model's data.

  #### Parameters

  - ##### key: string

    a unique name for this ElementReference

  #### Returns LocalElementReference

  An empty `LocalElementReference` which can then be bound to one or more elements.

### emitLocalEvents

- emitLocalEvents(): boolean
- emitLocalEvents(emit: boolean): void

- Returns true if local changes to this model are being emitted.

  FIXME inconsistent with isOpen()

  #### Returns boolean

- Toggles the `emitLocalEvents` setting. If set to `true`, whenever any data within this model is mutated, the same events will be emitted as if the mutation happened remotely. This is useful for handling change events in one place, switching on the IConvergenceModelValueEvent.local field.

  #### Parameters

  - ##### emit: boolean

    true if local changes to this model should emit

  #### Returns void

### endBatch

- endBatch(): void

- deprecated

  #### Returns void

### events

- events(): Observable<IConvergenceEvent>

- Inherited from ConvergenceEventEmitter.events

  Provides the events emitted by this object as an Observable stream.

  example

        eventEmitter.events()
          .filter(e => e.name === "myevent")
          .subscribe(e => console.log(e));

  #### Returns Observable<IConvergenceEvent>

  An Observable stream of all events emitted by this object.

### isBatchStarted

- isBatchStarted(): boolean

- Returns true if this model is currently in batch mode, that is, `startBatch` was called and the batch was not completed or cancelled.

  #### Returns boolean

  True if a batch is in progress, false otherwise.

### isClosing

- isClosing(): boolean

- Informs clients if the model is closing.

  #### Returns boolean

  True if the model is closing.

### isCommitted

- isCommitted(): boolean

- Determines if all local changes (if any) have been committed by the server.

  #### Returns boolean

  true if the model has no unacknowledged changes, false otherwise.

### isLocal

- isLocal(): boolean

- Determines is this model is a model that was created offline, and has not been synced to the server yet.

  experimental

  #### Returns boolean

  True if the model was created locally and has not synced to the server, false otherwise.

### isOpen

- isOpen(): boolean

- Returns true if this model is currently open.

  #### Returns boolean

### isSubscribedOffline

- isSubscribedOffline(): boolean

- Determines if this model is marked to be made available offline.

  experimental

  #### Returns boolean

  True if the model is marked for offline availability, false otherwise.

### maxTime

- maxTime(): Date

- Implementation of ObservableModel.maxTime

  Alias of time

  #### Returns Date

### maxVersion

- maxVersion(): number

- Implementation of ObservableModel.maxVersion

  Alias for version

  #### Returns number

### minTime

- minTime(): Date

- Implementation of ObservableModel.minTime

  Alias of createdTime

  #### Returns Date

### minVersion

- minVersion(): number

- Implementation of ObservableModel.minVersion

  The first version of the model (`0`).

  #### Returns number

### modelId

- modelId(): string

- Implementation of ObservableModel.modelId

  This model's ID, unique in the Domain.

  #### Returns string

### off

- off(event: string, listener: ConvergenceEventListener<IConvergenceEvent>): ConvergenceEventEmitter<IConvergenceEvent>

- Inherited from ConvergenceEventEmitter.off

  Removes a single event listener for a specific event.

  #### Parameters

  - ##### event: string

    The name of the event to remove the listener for.

  - ##### listener: ConvergenceEventListener<IConvergenceEvent>

    The listener callback to unregister.

  #### Returns ConvergenceEventEmitter<IConvergenceEvent>

  This object, in support of a fluent API.

### on

- on(event: string, listener: ConvergenceEventListener<IConvergenceEvent>): ConvergenceEventEmitter<IConvergenceEvent>

- Inherited from ConvergenceEventEmitter.on

  Adds a new event listener for the specified event. The class will ignore duplicate registrations of the same listener to the same event.

  #### Parameters

  - ##### event: string

    The name of the event to add the listener for.

  - ##### listener: ConvergenceEventListener<IConvergenceEvent>

    The listener callback to register.

  #### Returns ConvergenceEventEmitter<IConvergenceEvent>

  This object, in support of a fluent API.

### once

- once(event: string, listener: ConvergenceEventListener<IConvergenceEvent>): ConvergenceEventEmitter<IConvergenceEvent>

- Inherited from ConvergenceEventEmitter.once

  Adds a single shot event listener for the specified event. The listener will be called the first time the specified event is fired after the event registration occurs, after which the registration will be removed and no further events will be passed to the listener.

  #### Parameters

  - ##### event: string

    The name of the event to add the listener for.

  - ##### listener: ConvergenceEventListener<IConvergenceEvent>

    The listener callback to register.

  #### Returns ConvergenceEventEmitter<IConvergenceEvent>

  This object, in support of a fluent API.

### permissions

- permissions(): ModelPermissions

- Returns the permissions the current user has on this model.

  #### Returns ModelPermissions

### permissionsManager

- permissionsManager(): ModelPermissionManager

- Returns a permission manager that can be used to manage global or per-user permissions on this model. Requires the `manage` permission.

  #### Returns ModelPermissionManager

### reference

- reference(sessionId: string, key: string): ModelReference<any>

- Returns the remote ModelReference created by the given `sessionId` with the unique name `key`, or `undefined` if no such reference exists.

  See Remote References in the developer guide.

  #### Parameters

  - ##### sessionId: string

    The session ID that created the reference

  - ##### key: string

    the reference's unique key

  #### Returns ModelReference<any>

  A model reference for thee specified sessionId and key.

### references

- references(filter?: ReferenceFilter): Array<ModelReference<any>>

- Returns any remote references that match the given filter. You can provide a single `key` which could return references from multiple users, `sessionId` which would return all of a particular user session's references, or both, which is really just the same as using the reference method.

  #### Parameters

  - ##### Optional filter: ReferenceFilter

    an object containing either a `sessionId`, `key`, or both

  #### Returns Array<ModelReference<any>>

  An array of remote ModelReferences, or an empty array if there were no matches.

### removeAllListeners

- removeAllListeners(): ConvergenceEventEmitter<IConvergenceEvent>

- Inherited from ConvergenceEventEmitter.removeAllListeners

  Removes all listeners for all events. This is useful for cleanup before disposing of this particular event emitter.

  #### Returns ConvergenceEventEmitter<IConvergenceEvent>

  This object, in support of a fluent API.

### removeListener

- removeListener(event: string, listener: ConvergenceEventListener<IConvergenceEvent>): ConvergenceEventEmitter<IConvergenceEvent>

- Inherited from ConvergenceEventEmitter.removeListener

  Removes a single event listener for a specific event.

  #### Parameters

  - ##### event: string

    The name of the event to remove the listener for.

  - ##### listener: ConvergenceEventListener<IConvergenceEvent>

    The listener callback to unregister.

  #### Returns ConvergenceEventEmitter<IConvergenceEvent>

  This object, in support of a fluent API.

### removeListeners

- removeListeners(event: string): ConvergenceEventEmitter<IConvergenceEvent>

- Inherited from ConvergenceEventEmitter.removeListeners

  Removes all listeners bound on the given event.

  #### Parameters

  - ##### event: string

    the name of the event to remove listeners for

  #### Returns ConvergenceEventEmitter<IConvergenceEvent>

  This object, in support of a fluent API.

### resynchronizingCollaborators

- resynchronizingCollaborators(): ModelCollaborator[]

- Returns an array of collaborators, or other users who currently have this model open.

  #### Returns ModelCollaborator[]

  an array of collaborators

### root

- root(): RealTimeObject

- Implementation of ObservableModel.root

  Returns a RealTimeObject wrapping the root node of this model's data. From here one can view or modify all or any part of the subtree at any level of granularity.

  #### Returns RealTimeObject

### session

- session(): ConvergenceSession

- Implementation of ObservableModel.session

  The session associated with this opened model.

  #### Returns ConvergenceSession

### startBatch

- startBatch(): void

- Sets a flag on this model to indicate that any subsequent changes will be batched into one atomic operation. See Batch Changes in the developer guide.

  Note that a single batch operation must be fully encompassed in a single JS event loop ("tick"). This is to guarantee that no incoming events can conflict with the batch job.

  #### Returns void

### subscribeOffline

- subscribeOffline(): Promise<void>

- Marks this model to be available offline.

  experimental

  #### Returns Promise<void>

### time

- time(): Date

- Implementation of ObservableModel.time

  The timestamp at which this model was last modified.

  #### Returns Date

### unsubscribeOffline

- unsubscribeOffline(): Promise<void>

- Marks this model no longer needs to be available offline.

  experimental

  #### Returns Promise<void>

### version

- version(): number

- Implementation of ObservableModel.version

  The current version of the model.

  #### Returns number

### whenClosed

- whenClosed(): Promise<void>

- #### Returns Promise<void>

  A promise the will be resolved when the model is closed. If the model is already closed, the promise will be immediately resolved.