Accessibility

Table of Contents

Using Remote Methods with Flash Media Server

Overview of Remote Methods

Flash Media Server provides you with several different ways to invoke functions in remote locations, such as your server, other servers, and connected clients. To pick the method that solves your particular task most efficiently, you should familiarize yourself with these mechanisms—some of which are a bit like a one-way street and do not allow you to receive any return values, whereas others offer asynchronous returns.

Using SharedObject.send

The SharedObject.send method works in conjunction with a remote shared object and allows you to invoke a method that is defined on the shared object. Anyone connected to the shared object is able to receive the .send request.

It works a bit like a radio station: you know that everyone who is tuned in to your station (connected to the remote shared object) will be able to hear you (when you broadcast using .send).

Therefore SharedObject.send is ideally suited for applications such as text chats, where you can send your message and feel confident that everyone who wants to see it can do so. Moreover, you do not care who or how many people are "listening in," nor do you need confirmation that they have received your message.

It should also be noted that you, as the "broadcaster," will receive your own remote method callback when you use SharedObject.send. This is one major difference between SharedObject.send and the next method on our list.

Using NetStream.send/Stream.send

The NetStream.send method (and its server-side sibling, Stream.send) are very similar to the SharedObject.send method. They too work like the radio station previously described. The main difference is that the .send method is attached to the NetStream object and is not a shared object.

To be able to broadcast messages over a NetStream object using .send, you must be the publisher of the stream. Subscribers of a particular NetStream object are not able to use the .send method. Moreover, the originator of the message does not receive his or her own request back.

One very powerful feature of the NetStream.send method is the fact that you can record the event. This means that when Flash Media Server records any .send requests issued during the lifetime of a NetStream object, it stores those requests as part of the recording at the exact time the request was sent. Upon playback of the recorded stream, FMS invokes the .send requests again just as they were during actual recording. This opens the door for powerful applications that synchronize events—for example, of a live webcast upon playback some time later.

Right now, the only way to record a live Flash video stream is by using Flash Media Server. In this context, I should note that the .send calls are invoked upon playback even if this recorded FLV file is later delivered using progressive download. Pretty neat!

Using NetConnection.call

While the .send method of both the shared object and NetStream object offer a mechanism to broadcast a request to a whole group of users, NetConnection.call is a little more personal because it targets just one party—which in our case is the server. The NetConnection.call method is a client's channel to the server, allowing you to invoke a server-side method that is also able to send you an optional return value.

For this reason, NetConnection.call is often used to inform the server of a client's state ("Hey server, I'm going to idle for a bit") or to request information that only the server would be able to supply ("Yo server, could you tell me how many other users are connected to you right now?").

Using Client.call

If you have grasped NetConnection.call, then you won't have many difficulties understanding the Client.call method. It's basically NetConnection.call in reverse.

Client.call is the server's way of invoking a method on a particular client. Just like NetConnection.call, it can return an optional response back to the server ("Hey client, I am about to kick you out of this chat—do you mind?").

Also Using SharedObject.onSync

If you are already experienced in developing applications for Flash Media Server, then you know that there is a fourth method that is similar to SharedObject.send, which allows you to distribute information to everyone connected to the shared object. The SharedObject.onSync method is invoked whenever a value of one of the data slots of the shared object changes.

While this is one of the most powerful features (in my humble opinion) of Flash Media Server, I do not count it as a way to invoke remote methods. Instead, it acts more like a storage box for data that automatically synchronizes with all connected clients. I will not cover the SharedObject.onSync method in this article, but I recommend that you see the Flash Media Server documentation if you want to find out more about it.