Recipe 17.6 Broadcasting Data to Remote Shared Object Clients

17.6.1 Problem

You want to send data to all the clients of a FlashCom application that are connected to a remote shared object.

17.6.2 Solution

Use the SharedObject.onSync( ) method to catch synchronization events, and use the SharedObject.send( ) method to broadcast messages without writing to the RSO.

17.6.3 Discussion

Every time the server-side data for a remote shared object changes, FlashCom attempts to synchronize the client-side data for all clients connected to that RSO. The SharedObject.onSync( ) method for a client-side remote shared object is automatically invoked whenever a synchronization attempt has occurred, so you can monitor all synchronization events. (See more on this in Recipe 16.8.) This is useful in situations in which a relatively small amount of data is shared among multiple clients. Whenever one client makes a change, all the other clients are automatically notified. The onSync( ) method works the same regardless of whether the remote shared object is persistent.

You can also manually invoke a client-side method on all clients connected to a remote shared object by using the SharedObject.send( ) method, and you can call the send( ) method from either a server-side or client-side remote shared object. In either case, the send( ) method requires that you specify the name of the method to invoke on the client(s). You should then ensure that the client movies have a method with that same name defined for the client-side remote shared object.

// Use send(  ) to invoke a method named clientMethod(  ) on all connected clients. The
// send(  ) method can be used on either a server-side or client-side RSO.

// The following code should exist within the client movie. It defines a method named
// clientMethod(  ) for the client-side RSO named my_r_so.
my_r_so.clientMethod = function (  ) {
  trace("clientMethod(  ) called");

Additionally, you can pass parameters to the client-side method, as follows:

// Invoke a method named clientMethod(  ) on all connected clients and pass the method
// two parameters: 6 and six.
my_r_so.send("clientMethod", 6, "six");

// The following code should exist within the client movie. This time, the
// clientMethod(  ) method expects two parameters.
my_r_so.clientMethod = function (numNum, numStr) {
  trace("clientMethod(  ) called with parameters: " + numNum + " " + numStr);

We've seen two ways to broadcast messages to connected clients using RSOs: either explicitly calling the send( ) method or implicitly relying on the onSync( ) method. It is important to determine when to use which technique.

Generally, it is not a good idea to write a lot of data to a single property of a remote shared object. As the amount of data saved to a property of the RSO grows, so does the latency in the application. For example, in a chat application it is not a good idea to write all the chat messages to a property of an RSO and rely on the onSync( ) method to update the clients. At first this will appear to work fine, but after a few users have joined the chat room and more messages are sent, things will get slower and slower because the entire chat history is synchronized each time someone sends a new message. Instead, you should use the send( ) method to broadcast each new message to all the connected clients. In that case, the new message is sent to all connected clients, but the chat history is not. The only drawback is that the chat history is not available to new, connecting clients. What you can do to remedy this is to also use a call( ) method to send each new message to the server and append each new message to a chat history application variable. This way, the information is available when you want to use it, but it is not automatically sent with each new message.

17.6.4 See Also

Recipe 14.15, Recipe 14.17, Recipe 16.6, and Recipe 16.8

    Part I: Local Recipes
    Part II: Remote Recipes