Server-Side Communication ActionScript > SharedObject (object)

 

SharedObject (object)

Shared objects let you share data between multiple client movies in real time. Shared objects can be persistent on the server and you can think of these objects as real-time data transfer devices. You can also use the client-side ActionScript SharedObject object to create shared objects on the client. For more information, see the SharedObject entry in the Client-Side Communication ActionScript Dictionary. The following list describes common ways to use remote shared objects:

Storing and sharing data on a server

A shared object can store data on the server for other clients to retrieve. For example, you can open a remote shared object, such as a phone list, that is persistent on the server. Whenever a client makes a change to the shared object, the revised data is available to all clients that are currently connected to the object or that connect to it later. If the object is also persistent locally and a client changes the data while not connected to the server, the changes are copied to the remote shared object the next time the client connects to the object.

Sharing data in real time

A shared object can share data among multiple clients in real time. For example, you can open a remote shared object that stores real-time data (such as a list of users connected to a chat room) that is visible to all clients connected to the object. When a user enters or leaves the chat room, the object is updated and all clients that are connected to the object see the revised list of chat room users.

Every shared object is identified by a unique name and contains a list of name-value pairs, called properties, just like any other ActionScript object. A name must be a unique string and a value can be any ActionScript data type. (For more information about data types, see Using Flash MX.) All shared objects have a data property. Any property of the data property can be shared and is called a slot.

A shared object can be owned by the current (local) application instance or by a different (remote) application instance. The remote application instance can be on the same server or on a different server. References to shared objects that are owned by a remote application instance are called proxied shared objects.

The slot of a shared object owned by the local instance can be modified by multiple clients or by the server simultaneously; there is no conflict on the server side when a shared object is modified. For example, a call to SharedObject.getProperty returns the latest value and setting a new value assigns a new value for the named slot and updates the object version. If you write a server-side script that modifies multiple properties, you can prevent other clients from modifying the object during the update, by calling the SharedObject.lock method before updating the object. Then you can call SharedObject.unlock to commit the changes and allow other changes to be made. Any change to a shared object results in notification to all the clients that are currently subscribed to the shared object.

When you get a reference to a shared object owned by a remote application instance, any changes made to the object are sent to the instance that owns the object. The success or failure of any changes are sent using the SharedObject.onSync event handler, if it is defined. Also, the SharedObject.lock and SharedObject.unlock methods cannot be used to lock or unlock proxied shared objects.

 
Method summary for the SharedObject object

Method

Description

SharedObject.clear

Deletes all properties of a persistent shared object.

SharedObject.close

Unsubscribes from a shared object.

SharedObject.flush

Causes the server to save the current state of a shared object.

SharedObject.get

Returns a reference to a shared object.

SharedObject.getProperty

Gets the value of a shared object property.

SharedObject.getPropertyNames

Returns an array of all the current valid properties in the shared object.

SharedObject.lock

Locks the shared object instance. Prevents any changes to this object by clients until the SharedObject.unlock method is called.

SharedObject.purge

Causes the server to remove all deleted properties that are older than the specified version.

SharedObject.send

Sends a message to the client subscribing to this shared object.

SharedObject.setProperty

Sets a new value for a shared object property.

SharedObject.size

Returns the number of valid properties in a shared object.

SharedObject.unlock

Unlocks a shared object instance that was locked with SharedObject.lock.


 
Property summary for the SharedObject object

Property

Description

SharedObject.name

The name of a shared object.

SharedObject.resyncDepth

The depth that indicates when the deleted values of a shared object should be permanently deleted.

SharedObject.version

The current version number of a shared object.


 
Event summary for the SharedObject object

Property

Description

SharedObject.handlerName

A placeholder for a property name that specifies a function object that is invoked when a shared object receives a broadcast message whose method name matches the property name.

SharedObject.onStatus

Reports errors, warnings, and status messages for a shared object.

SharedObject.onSync

Invoked when a shared object changes.