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.
The SharedObject class lets you store data on the server and share data between multiple client applications in real time. Shared objects can be temporary, or they can persist on the server after an application has closed; you can consider shared objects as real-time data transfer devices.
The following information explains the server-side SharedObject class. You can also create shared objects with the client-side SharedObject class.
The following list describes common ways to use shared objects in Server-Side ActionScript:
-
-
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 that is visible to all clients connected to the object, such as a list of users connected to a chat room. 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.
It is important to understand the following information about using shared objects in Server-Side ActionScript:
The Server-Side ActionScript method SharedObject.get() creates remote shared objects; there is no server-side method that creates local shared objects. Local shared objects are stored in memory, unless they’re persistent, in which case they are stored in .sol files.
Remote shared objects that are stored on the server have the file extension .fso and are stored in a subdirectory of the application that created them. Remote shared objects on the client have the file extension .sor and are also stored in a subdirectory of the application that created them.
Server-side shared objects can be nonpersistent (that is, they exist for the duration of an application instance) or persistent (that is, they are stored on the server after an application closes).
To create a persistent shared object, set the persistence parameter of the SharedObject.get() method to true. Persistent shared objects let you maintain an application’s state.
-
Every remote shared object is identified by a unique name and contains a list of name-value pairs, called properties, like any other ActionScript object. A name must be a unique string and a value can be any ActionScript data type.
Note:Unlike client-side shared objects, server-side shared objects do not have a data property.
To get the value of a server-side shared object property, call SharedObject.getProperty(). To set the value of a server-side shared object property, call SharedObject.setProperty().
To clear a shared object, call the SharedObject.clear()method; to delete multiple shared objects, call the application.clearSharedObjects() method.
Server-side shared objects can be owned by the current application instance or by another application instance. The other application instance can be on the same server or on a different server. References to shared objects that are owned by a different application instance are called proxied shared objects.
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. Call SharedObject.mark() to deliver change events in groups within the lock() and unlock() methods.
When you get a reference to a proxied shared object, any changes made to the object are sent to the instance that owns the object. The success or failure of any changes is sent by using the SharedObject.onSync() event handler, if it is defined.
The SharedObject.lock() and SharedObject.unlock() methods cannot lock or unlock proxied shared objects.
Availability
Flash Communication Server 1
Property summary
Property |
Description |
A boolean value indicating whether the server periodically stores all persistent shared objects (true) or not (false). |
|
Read-only; a boolean value indicating whether the persistent shared object has been modified since the last time it was stored (true) or not (false). |
|
Read-only; the name of a shared object. |
|
An integer that indicates when the deleted values of a shared object should be permanently deleted. |
|
Read-only; the current version number of a shared object. |
Method summary
Method |
Description |
---|---|
Deletes all the properties of a single shared object and sends a clear event to all clients that subscribe to a persistent shared object. |
|
Detaches a reference from a shared object. |
|
Static; stores either a specific persistent shared object instance or all persistent shared object instances with an isDirty property whose value is true. |
|
Saves the current state of a persistent shared object. |
|
Static; creates a shared object or returns a reference to an existing shared object. |
|
Retrieves the value of a named property in a shared object. |
|
Enumerates all the property names for a given shared object. |
|
Locks a shared object. |
|
Delivers all change events to a subscribing client as a single message. |
|
Causes the server to remove all deleted properties that are older than the specified version. |
|
Executes a method in a client-side script. |
|
Updates the value of a property in a shared object. |
|
Returns the total number of valid properties in a shared object. |
|
Allows other clients to update the shared object. |
Event handler summary
Event handler |
Description |
An event handler invoked when a shared object receives a message with the same name from the client-side SharedObject.send() method. |
|
Invoked when errors, warnings, and status messages associated with either a local instance of a shared object or a persistent shared object occur. |
|
Invoked when a shared object changes. |
SharedObject.autoCommit
so.autoCommit
A boolean value indicating whether the server periodically stores all persistent shared objects (true) or not (false). If autoCommit is false, the application must call SharedObject.commit() to save the shared object; otherwise, the data is lost.
This property is true by default. To override the default, specify the initial state by using the following configuration key in the Application.xml file, as shown in the following example:
<SharedObjManager> <AutoCommit>false</AutoCommit> </SharedObjManager>
Availability
Flash Media Server 2
SharedObject.clear()
so.clear()
Deletes all the properties of a single shared object and sends a clear event to all clients that subscribe to a persistent shared object. The persistent data object is also removed from a persistent shared object.
Availability
Flash Communication Server 1
Returns
Returns true if successful; otherwise, false.
See also
SharedObject.close()
so.close()
Detaches a reference from a shared object. A call to the SharedObject.get() method returns a reference to a shared object instance. The reference is valid until the variable that holds the reference is no longer in use and the script is garbage collected. To destroy a reference immediately, you can call SharedObject.close(). You can use SharedObject.close() when you no longer want to proxy a shared object.
Availability
Flash Communication Server 1
Example
In the following example, so is attached as a reference to shared object foo. When you call so.close(), you detach the reference so from the shared object foo.
so = SharedObject.get("foo"); // Insert code here. so.close();
See also
SharedObject.commit()
so.commit([name])
Static; stores either a specific persistent shared object instance or all persistent shared object instances with an isDirty property whose value is true. Use this method if the SharedObject.autoCommit property is false and you need to manage when a shared object is stored locally.
Availability
Flash Media Server 2
Parameters
name
A string indicating the name of the persistent shared object instance to store. If no name is specified, or if an empty string is passed, all persistent shared objects are stored. This parameter is optional.
Returns
A boolean value indicating success (true) or failure (false).
Example
The following code commits all dirty shared objects to local storage when the application stops:
application.onAppStop = function (info){ // Insert code here. SharedObject.commit(); }
SharedObject.flush()
so.flush()
Saves the current state of a persistent shared object. Invokes the SharedObject.onStatus() handler and passes it an object that contains information about the success or failure of the call.
Availability
Flash Communication Server 1
Returns
A boolean value of true if successful; otherwise, false.
Example
The following example places a reference to the shared object foo in the variable so. It then locks the shared object instance so that no one can make any changes to it and saves the shared object by calling so.flush(). After the shared object is saved, it is unlocked so that further changes can be made.
var so = SharedObject.get("foo", true); so.lock(); // Insert code here that operates on the shared object. so.flush(); so.unlock();
SharedObject.get()
SharedObject.get(name, persistence [, netConnection])
Static; creates a shared object or returns a reference to an existing shared object. To perform any operation on a shared object, the server-side script must get a reference to the shared object by using the SharedObject.get() method. If the requested object is not found, a new instance is created.
Availability
Flash Communication Server 1
Parameters
name
Name of the shared object instance to return.
persistence
A boolean value: true for a persistent shared object; false for a nonpersistent shared object. If no value is specified, the default value is false.
netConnection
A NetConnection object that represents a connection to an application instance. You can pass this parameter to get a reference to a shared object on another server or a shared object that is owned by another application instance. All update notifications for the shared object specified by the name parameter are proxied to this instance, and the remote instance notifies the local instance when a persistent shared object changes. The NetConnection object that is used as the netConnection parameter does not need to be connected when you call SharedObject.get(). The server connects to the remote shared object when the NetConnection state changes to connected. This parameter is optional.
Returns
A reference to an instance of the SharedObject class.
Details
There are two types of shared objects, persistent and nonpersistent, and they have separate namespaces. This means that a persistent and a nonpersistent shared object can have the same name and exist as two distinct shared objects. Shared objects are scoped to the namespace of the application instance and are identified by a string. The shared object names should conform to the URI specification.
You can also call SharedObject.get() to get a reference to a shared object that is in a namespace of another application instance. This instance can be on the same server or on a different server and is called a proxied shared object. To get a reference to a shared object from another instance, create a NetConnection object and use the NetConnection.connect() method to connect to the application instance that owns the shared object. Pass the NetConnection object as the netConnection parameter of the SharedObject.get() method. The server-side script must get a reference to a proxied shared object before there is a request for the shared object from any client. To do this, call SharedObject.get() in the application.onAppStart() handler.
If you call SharedObject.get() with a netConnection parameter and the local application instance already has a shared object with the same name, the shared object is converted to a proxied shared object. All shared object messages for clients that are connected to a proxied shared object are sent to the master instance.
If the connection state of the NetConnection object that was used as the netConnection parameter changes state from connected to disconnected, the proxied shared object is set to idle and any messages received from subscribers are discarded. The NetConnection.onStatus() handler is called when a connection is lost. You can then reestablish a connection to the remote instance and call SharedObject.get(), which changes the state of the proxied shared object from idle to connected.
If you call SharedObject.get() with a new NetConnection object on a proxied shared object that is already connected, and if the URI of the new NetConnection object doesn’t match the current NetConnection object, the proxied shared object unsubscribes from the previous shared object, sends a clear event to all subscribers, and subscribes to the new shared object instance. When a subscribe operation to a proxied shared object is successful, all subscribers are reinitialized to the new state. This process lets you migrate a shared object from one application instance to another without disconnecting the clients.
Updates received by proxied shared objects from subscribers are checked to see if the update can be rejected based on the current state of the proxied shared object version and the version of the subscriber. If the change can be rejected, the proxied shared object doesn’t forward the message to the remote instance; the reject message is sent to the subscriber.
The corresponding client-side ActionScript method is SharedObject.getRemote().
Example
The following example creates a shared object named foo in the function onProcessCmd(). The function is passed a parameter, cmd, that is assigned to a property in the shared object.
function onProcessCmd(cmd){ // Insert code here. var shObj = SharedObject.get("foo", true); propName = cmd.name; shObj.getProperty (propName, cmd.newAddress); }
The following example uses a proxied shared object. A proxied shared object resides on a server or in an application instance (called master) that is different from the server or application instance that the client connects to (called proxy). When the client connects to the proxy and gets a remote shared object, the proxy connects to the master and gives the client a reference to this shared object. The following code is in the main.asc file:
application.appStart = function() { nc = new NetConnection(); nc.connect("rtmp://" + master_server + "/" + master_instance); proxySO = SharedObject.get("myProxy",true,nc); // Now, whenever the client asks for a persistent // shared object called myProxy, it receives themyProxy // shared object from master_server/master_instance. };
SharedObject.getProperty()
so.getProperty(name)
Retrieves the value of a named property in a shared object. The returned value is a copy associated with the property, and any changes made to the returned value do not update the shared object. To update a property, use the SharedObject.setProperty() method.
Availability
Flash Communication Server 1
Parameters
name
A string indicating the name of a property in a shared object.
Returns
The value of a SharedObject property. If the property doesn’t exist, returns null.
Example
The following example gets the value of the name property on the user shared object and assigns it to the firstName variable:
firstName = user.getProperty("name");
See also
SharedObject.getPropertyNames()
so.getPropertyNames()
Enumerates all the property names for a given shared object.
Availability
Flash Communication Server 1
Returns
An array of strings that contain all the property names of a shared object.
Example
The following example calls getPropertyNames() on the myInfo shared object and places the names in the names variable. It then enumerates those property names in a for loop.
myInfo = SharedObject.get("foo"); var addr = myInfo.getProperty("address"); myInfo.setProperty("city", "San Francisco"); var names = myInfo.getPropertyNames(); for (x in names){ var propVal = myInfo.getProperty(names[x]); trace("Value of property " + names[x] + " = " + propVal); }
SharedObject.handlerName()
so.handlerName = function([p1,..., pN]){}
An event handler invoked when a shared object receives a message with the same name from the client-side SharedObject.send() method. You must define a Function object and assign it to the event handler.
The this keyword used in the body of the function is set to the shared object instance returned by SharedObject.get().
If you don’t want the server to receive a particular message, do not define this handler.
Availability
Flash Communication Server 1
Parameters
p1, ..., pN
Optional parameters passed to the handler method if the message contains user-defined parameters. These parameters are the user-defined objects that are passed to the SharedObject.send() method.
Returns
Any return value is ignored by the server.
Example
The following example defines an event handler called traceArgs:
var so = SharedObject.get("userList", false); so.traceArgs = function(msg1, msg2){ trace(msg1 + " : " + msg2); };
SharedObject.isDirty
so.isDirty
Read-only; a boolean value indicating whether a persistent shared object has been modified since the last time it was stored (true) or not (false). The SharedObject.commit() method stores shared objects with an isDirty property that is true.
This property is always false for nonpersistent shared objects.
Availability
Flash Media Server 2
Example
The following example saves the so shared object if it has been changed:
var so = SharedObject.get("foo", true); if (so.isDirty){ SharedObject.commit(so.name); }
SharedObject.lock()
so.lock()
Locks a shared object. This method gives the server-side script exclusive access to the shared object; when the SharedObject.unlock() method is called, all changes are batched and one update message is sent through the SharedObject.onSync() handler to all the clients that subscribe to this shared object. If you nest the SharedObject.lock() and SharedObject.unlock() methods, make sure that there is an unlock() method for every lock() method; otherwise, clients are blocked from accessing the shared object.
You cannot use the SharedObject.lock() method on proxied shared objects.
Availability
Flash Communication Server 1
Returns
An integer indicating the lock count: 0 or greater indicates success; -1 indicates failure. For proxied shared objects, always returns -1.
Example
The following example locks the so shared object, executes the code that is to be inserted, and then unlocks the object:
var so = SharedObject.get("foo"); so.lock(); // Insert code here that operates on the shared object. so.unlock();
SharedObject.mark()
so.mark(handlerName, p1, ..., pN)
Delivers all change events to a subscribing client as a single message.
In a server-side script, you can call the SharedObject.setProperty() method to update multiple shared object properties between a call to the lock() and unlock() methods. All subscribing clients receive a change event notification through the SharedObject.onSync() handler. However, because the server may collapse multiple messages to optimize bandwidth, the change event notifications may not be sent in the same order as they were in the code.
Use the mark() method to execute code after all the properties in a set have been updated. You can call the handlerName parameter passed to the mark() method, knowing that all property changes before the mark() call have been updated.
Availability
Flash Media Server 2
Parameters
handlerName
Calls the specified handler on the client-side SharedObject instance. For example, if the handlerName parameter is onChange, the client invokes the SharedObject.onChange() handler with all the p1, ...,pN parameters.
Note: Do not use a built-in method name for a handler name. For example, if the handler name is close, the subscribing stream will be closed.
p1, ..., pN
Parameters of any ActionScript type, including references to other ActionScript objects. Parameters are passed to handlerName when it is executed on the client.
Returns
A boolean value. Returns true if the message can be dispatched to the client; otherwise, false.
Example
The following example calls the mark() method twice to group two sets of shared object property updates for clients:
var myShared = SharedObject.get("foo", true); myShared.lock(); myShared.setProperty("name", "Stephen"); myShared.setProperty("address", "Xyz lane"); myShared.setProperty("city", "SF"); myShared.mark("onAdrChange", "name"); myShared.setProperty("account", 12345); myShared.mark("onActChange"); myShared.unlock();
The following example shows the receiving client-side script:
connection = new NetConnection(); connection.connect("rtmp://flashmediaserver/someApp"); var x = SharedObject.get( "foo", connection.uri, true); x.connect(connection); x.onAdrChange = function(str) { // Shared object has been updated, // can look at the “name”, “address” and “city” now. } x.onActChange = function(str) { // Shared object has been updated, // can look at the “account” property now, }
SharedObject.name
so.name
Read-only; the name of a shared object.
Availability
Flash Communication Server 1
SharedObject.onStatus()
so.onStatus = function(info) {}
Invoked when errors, warnings, and status messages associated with either a local instance of a shared object or a persistent shared object occur.
Availability
Flash Communication Server 1
Parameters
info
An information object.
Example
The following client-side code defines an anonymous function that just traces the level and code properties of the specified shared object:
so = SharedObject.get("foo", true); so.onStatus = function(infoObj){ //Handle status messages passed in infoObj. trace(infoObj.level + “; “ + infoObj.code); };
SharedObject.onSync()
so.onSync = function(list){}
Invoked when a shared object changes. Use the onSync() handler to define a function that handles changes made to a shared object by subscribers.
For proxied shared objects, defines the function to get the status of changes made by the server and other subscribers.
You cannot define the onSync() handler on the prototype property of the SharedObject class in Server-Side ActionScript.
Availability
Flash Communication Server 1
Parameters
list
An array of objects that contain information about the properties of a shared object that have changed since the last time the onSync() handler was called. The notifications for proxied shared objects are different from the notifications for shared objects that are owned by the local application instance. The following table describes the codes for local shared objects:
Local code |
Meaning |
---|---|
change |
A property was changed by a subscriber. |
delete |
A property was deleted by a subscriber. |
name |
The name of a property that has changed or been deleted. |
oldValue |
The old value of a property. This is true for both change and delete messages; on the client, oldValue is not set for delete. |
Note: Changing or deleting a property on the server side by using the SharedObject.setProperty() method always succeeds, so there is no notification of these changes.
The following table describes the codes for local shared objects:
Proxied code |
Meaning |
---|---|
success |
A server change of the shared object was accepted. |
reject |
A server change of the shared object was rejected. The value on the remote instance was not changed. |
change |
A property was changed by another subscriber. |
delete |
A property was deleted. This notification can occur when a server deletes a shared object or if another subscriber deletes a property. |
clear |
All the properties of a shared object are deleted. This can happen when the server’s shared object is out of sync with the master shared object or when the persistent shared object migrates from one instance to another. This event is typically followed by a change message to restore all of the server’s shared object properties. |
name |
The name of a property that has changed or been deleted. |
oldValue |
The old value of the property. This is valid only for the reject, change, and delete codes. |
Note: The SharedObject.onSync() handler is invoked when a shared object has been successfully synchronized with the server. If there is no change in the shared object, the list object may be empty.
Example
The following example creates a function that is invoked whenever a property of the shared object so changes:
// Create a new NetConnection object. nc = new NetConnection(); nc.connect("rtmp://server1.xyx.com/myApp"); // Create the shared object. so = SharedObject.get("MasterUserList", true, nc); // The list parameter is an array of objects containing information // about successfully or unsuccessfully changed properties // from the last time onSync() was called. so.onSync = function(list) { for (var i = 0; i < list.length; i++) { switch (list[i].code ) { case "success": trace ("success"); break; case "change": trace ("change"); break; case "reject": trace ("reject"); break; case "delete": trace ("delete"); break; case "clear": trace ("clear"); break; } } };
SharedObject.purge()
so.purge(version)
Causes the server to remove all deleted properties that are older than the specified version. Although you can also accomplish this task by setting the SharedObject.resyncDepth property, the purge() method gives the script more control over which properties to delete.
Availability
Flash Communication Server 1
Parameters
version
A number indicating the version. All deleted data that is older than this version is removed.
Returns
A boolean value.
Example
The following example deletes all the properties of the so shared object that are older than the value of so.version - 3:
var so = SharedObject.get("foo", true); so.lock(); so.purge(so.version - 3); so.unlock();
SharedObject.resyncDepth
so.resyncDepth
An integer that indicates when the deleted properties of a shared object should be permanently deleted. You can use this property in a server-side script to resynchronize shared objects and to control when shared objects are deleted. The default value is infinity.
If the current revision number of the shared object minus the revision number of the deleted property is greater than the value of SharedObject.resyncDepth, the property is deleted. Also, if a client connecting to this shared object has a client revision that, when added to the value of SharedObject.resyncDepth, is less than the value of the current revision on the server, all the current elements of the client shared object are deleted, the valid properties are sent to the client, and the client receives a “clear” message.
This method is useful when you add and delete many properties and you don’t want to send too many messages to the client. Suppose that a client is connected to a shared object that has 12 properties and then disconnects. After that client disconnects, other clients that are connected to the shared object delete 20 properties and add 10 properties. When the client reconnects, it could, for example, receive a delete message for the 10 properties it previously had and then a change message on two properties. You can use SharedObject.resyncDepth property to send a “clear” message, followed by a change message for two properties, which saves the client from receiving 10 delete messages.
Availability
Flash Communication Server 1
Example
The following example resynchronizes the shared object so if the revision number difference is greater than 10:
so = SharedObject.get("foo"); so.resyncDepth = 10;
SharedObject.send()
so.send(methodName, [p1, ..., pN])
Executes a method in a client-side script. You can use SharedObject.send() to asynchronously execute a method on all the Flash clients subscribing to a shared object. The server does not receive any notification from the client on the success, failure, or return value in response to this message.
Availability
Flash Communication Server 1
Parameters
methodName
A string indicating the name of a method on a client-side shared object. For example, if you specify "doSomething", the client must invoke the SharedObject.doSomething() method, with all the p1, ..., pN parameters.
p1, ..., pN
Parameters of any type, including references to other objects. These parameters are passed to the specified methodName on the client.
Returns
A boolean value of true if the message was sent to the client; otherwise, false.
Example
The following example calls the SharedObject.send() method to invoke the doSomething() method on the client and passes the string "This is a test":
var so = SharedObject.get("foo", true); so.send("doSomething", "This is a test");
The following example is the client-side ActionScript code that defines the doSomething() method:
nc = new NetConnection(); nc.connect("rtmp://www.adobe.com/someApp"); var so = SharedObject.getRemote("foo", nc.uri, true); so.connect(nc); so.doSomething = function(str) { // Process the str object. };
SharedObject.setProperty()
so.setProperty(name, value)
Updates the value of a property in a shared object.
The name parameter on the server side is the same as an attribute of the data property on the client side. For example, the following two lines of code are equivalent; the first line is Server-Side ActionScript and the second is client-side ActionScript:
so.setProperty(nameVal, "foo"); clientSO.data[nameVal] = "foo";
A shared object property can be modified by a client between successive calls to SharedObject.getProperty() and SharedObject.setProperty(). If you want to preserve transactional integrity, call the SharedObject.lock() method before modifying the shared object; be sure to call SharedObject.unlock() when you finish making modifications. If you call SharedObject.setProperty() without first calling SharedObject.lock(), the change is made to the shared object, and all object subscribers are notified before SharedObject.setProperty() returns. If you call SharedObject.lock() before you call SharedObject.setProperty(), all changes are batched and sent when the SharedObject.unlock() method is called. The SharedObject.onSync() handler on the client side is invoked when the local copy of the shared object is updated.
If only one source (whether client or server) is updating a shared object in a server-side script, you don’t need to use the lock() or unlock() method or the onSync() handler.
Availability
Flash Communication Server 1
Parameters
name
The name of the property in the shared object.
value
An ActionScript object associated with the property, or null to delete the property.
Example
The following example uses the SharedObject.setProperty() method to create the city property with the value San Francisco. It then enumerates all the property values in a for loop and calls trace() to display the values.
myInfo = SharedObject.get("foo"); var addr = myInfo.getProperty("address"); myInfo.setProperty("city", "San Francisco"); var names = sharedInfo.getPropertyNames(); for (x in names){ var propVal = sharedInfo.getProperty(names[x]); trace("Value of property " + names[x] + " = " + propVal); }
See also
SharedObject.size()
so.size()
Returns the total number of valid properties in a shared object.
Availability
Flash Communication Server 1
Returns
An integer indicating the number of properties.
Example
The following example gets the number of properties of a shared object and assigns that number to the variable len:
var so = SharedObject.get("foo", true); var soLength = so.size();
SharedObject.unlock()
so.unlock()
Allows other clients to update the shared object. A call to this method also causes the server to commit all changes made after the SharedObject.lock() method is called and sends an update message to all clients.
You cannot call the SharedObject.unlock() method on proxied shared objects.
Availability
Flash Communication Server 1
Returns
An integer indicating the lock count: 0 or greater if successful; -1 otherwise. For proxied shared objects, this method always returns -1.
Example
The following example unlocks a shared object:
var so = SharedObject.get("foo", true); so.lock(); // Insert code to manipulate the shared object. so.unlock();
See also
SharedObject.version
so.version
Read-only; the current version number of the shared object. Calls to the SharedObject.setProperty() method on either the client or the server increment the value of the version property.
Availability
Flash Communication Server 1