Tom Krcha's FlashRealtime

Hey amigo! These are my notes. I'm Platform Evangelist with Adobe.


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:

No Comments »

No comments yet.

RSS feed for comments on this post. / TrackBack URL

Leave a comment