Tom Krcha's FlashRealtime

Hey amigo!
I am Tom Krcha, Gaming Evangelist at Adobe. These are my notes


SharedProperty – AFCS best practices

July 8th, 2009

SharedProperty is very useful class in Adobe Flash Collaboration Service, which simplifies all data sharing and shared data binding. The core of SharedProperty is based on CollectionNode.

If you are familiar with Flash Media Server – SharedProperty is similar to Remote SharedObject, however you can’t define function on SharedProperty – on SharedObject you can.

Common use cases for SharedProperty in Collaboration apps:

  • - shared parameters of components (size, zoom/scale, position, scrolling)
  • - shared text selection
  • - shared mouse position
  • - shared radio button toggle
  • - shared location/navigation
  • - everything you want to make realtime changeable by others
  • - and more…

SharedProperty also reflects all access models and rules you use in AFCS.

The class you will also you a lot in your collaboration apps is SharedCollection. SharedCollection is complex shared data structure – in other words ArrayCollection of SharedProperties, but still different.

There are two usage approaches – ActionScript based and MXML based:

ActionScript: classic approach, but might become very verbose – normally you have around 5-10 SharedProperties in a tiny collaboration app.

5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import com.adobe.rtc.events.SessionEvent;
import com.adobe.rtc.events.SharedPropertyEvent;
import com.adobe.rtc.sharedModel.SharedProperty;
 
private var sharedProperty:SharedProperty;
 
private function initSharedProperty():void{
	sharedProperty = new SharedProperty();
	sharedProperty.sharedID = "_someSharedPropertyID";
	sharedProperty.connectSession = connectSession;
	sharedProperty.addEventListener(SharedPropertyEvent.CHANGE,onSharedPropertyChange);
	sharedProperty.subscribe();
}
 
private function onSharedPropertyChange(event:SharedPropertyEvent):void{
	btn.label = sharedProperty.value;
}
 
private function onSessionSync(event:SessionEvent):void{
	if(connectSession.isSynchronized){
		initSharedProperty();
	}
}

Download link: SharedPropertiesExample1.mxml

You can also define SharedProperty in MXML, which is sometimes much more effective. See the binding of sharedProperty.value on Button label.

<sharedModel:SharedProperty id="sharedProperty" 
                        connectSession="{connectSession}"
                        sharedID="_someSharedPropertyID" 
                        change="onSharedChange(event)"   />
 
<mx:Button id="btn" label="{sharedProperty.value}" 
         click="{sharedProperty.value='Hello'+Math.random()}" />

But make sure you don’t forget to subscribe() sharedProperty.

private function onSessionSync(event:SessionEvent):void{
	if(connectSession.isSynchronized){
		sharedProperty.subscribe();
	}
}

Download link: SharedPropertiesExample2.mxml

My favorite is MXML one – although it’s way too effective – you’ll find this method bit awkward as well – as it does not subscribe automatically once ConnectSession becomes synchronized.
So for this purpose I have created class SharedPropertyAuto.as, which has parameter autoSubscribe:Boolean (true by default).

With SharedPropertyAuto you don’t need to manually subscribe, so it saves lot of code, when your collaboration app becomes bigger. See – beautiful final solution on few lines of code with realtime shared property:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:rtc="AfcsNameSpace" xmlns:sharedModel="com.adobe.rtc.sharedModel.*" xmlns:local="*">
	<mx:Button id="btn" label="{sharedProperty.value}" click="{sharedProperty.value='Hello'+Math.random()}" />
	<local:SharedPropertyAuto id="sharedProperty" connectSession="{connectSession}" sharedID="_someSharedPropertyID" />
	<rtc:AdobeHSAuthenticator id="auth" userName="**********" password="*********" />
	<rtc:ConnectSessionContainer id="connectSession" authenticator="{auth}" roomURL="**********" />
</mx:Application>

Download link: SharedPropertiesExample3.mxml, SharedPropertyAuto.as

Also maybe in the future – autoSubscribe feature will become the part of SharedProperty, till then – you can use SharedPropertyAuto.

Download source code: SharedPropertiesAll.zip

Facebook comments:

2 Comments »

  1. An interesting discussion is worth comment. I think that you should write more on this topic, it might not be a taboo subject but generally people are not enough to speak on such topics. To the next. Cheers

    Comment by foteliki samochodowe dzieci — November 29, 2010 @ 8:05 pm

  2. I’m impressed, I need to say. Really rarely do I encounter a weblog that’s each educative and entertaining, and let me let you know, you may have hit the nail on the head. Your concept is excellent; the issue is one thing that not enough persons are talking intelligently about. I am very happy that I stumbled across this in my search for something referring to this.

    Comment by Hq Motorcycle Helmet — January 19, 2011 @ 2:06 am

RSS feed for comments on this post. / TrackBack URL

Leave a comment

Comment moderation is enabled. Your comment may take some time to appear.