PureMVC Architects Lounge

Announcements and General Discussion => General Discussion => Topic started by: coulix on October 12, 2008, 03:20:39

Title: Concept Question, proxy and vo for a chat
Post by: coulix on October 12, 2008, 03:20:39

Firstly thank you for creating such a framework it forces good practices and unless you are a Pattern/OO wizard we need it !

I am new to AS3/Flex/pureMVC, in order to test my understanding of the framework i am trying to build yet an other chat with Flex/FMS and server side shared objects.

Please tell me if there is anything wrong in the following facts:
Simple case its a 1-1 chat.

- I dont need to store the messages received and typed in a messagesVO as an arrayCollection of String, i will just update the text widget of my Chat View Component by cummulating the chatmessage string attached to a notification sent by the proxy?

- I need a ChatProxy which will be an async remote proxy interfacing the remote shared object ?

- In a pureMVC jabber Chat demo around the author uses a custom chatEvent holding an id and a string, is there a need for custom events when we can pass Array as notification body. I guess there is an obvious use for it, but an example may be welcome.

If somehow i manage to build this small app after review i will add it to the contributions.


Title: Re: Concept Question, proxy and vo for a chat
Post by: Joel Hooks on October 12, 2008, 09:53:10
is there a need for custom events when we can pass Array as notification body.

I think you can almost ALWAYS toss around generic objects ( [], {}, etc ). It will probably save you a few minutes up front, but why not create the VOs and have type safe messages being tossed around that are clearly defined against a documented interface (yay code completion!)?

In my simple chat application I am using a database, so it is a little different, but I keep a ChatSessionVO and ChatMessageVO going so that I can have nice logs and offline messaging and whatnot.

Title: Re: Concept Question, proxy and vo for a chat
Post by: coulix on October 12, 2008, 10:16:32
Thanks for these clarifications, It must have been my Python heritage that fooled me: no Strong Typing no ide and Magic ponies.

Title: Re: Concept Question, proxy and vo for a chat
Post by: Joel Hooks on October 13, 2008, 05:34:10
I use Komodo for Python, it provides a lot of nice code completion, but ya, contextually speaking it is a big jump from python to actionscript.

They play really well together though. I tried to get into Java for my service tier, but I am back to Python. It is such a pleasant language to use :>

Title: Re: Concept Question, proxy and vo for a chat
Post by: puremvc on October 13, 2008, 08:13:42
@coulix From a 'simple as possible but no simpler' standpoint I'm in agreement with you. But from a 'buiding maintainable software' standpoint I'm definitely in agreement with Joel.

For your simply stated requirements, you've got it covered. But almost never is a piece of software concieved, written and then frozen. That was the initial goal with PureMVC, but it didn't stay at 1.0 for long. Point is, when you go to add the next feature, or the one after that, future you will think of past you as his best compadre for making it easy to extend.

And this yaya from a guy who sling Perl in vi for years. I, too forked the sign of the evil eye to the magic ponies.

But when a job situation forced me into Java and Eclipse (kicking and screaming the whole time, mind you), I finally saw the light. Once I really got OOP and the power of strong typing combined with an IDE that knew everything there is to know about the language, my productivity shot through the roof and my code became more solid and understandable by orders of magnitude.
Come to the dark side. We have cookies :)

Title: Re: Concept Question, proxy and vo for a chat
Post by: coulix on October 14, 2008, 02:09:19
Thanks for these tips, i am trying hard to find the way of good coding practices.

I got the Chat app working in a pureMVC way, but before i dare to show the code here i have few questions on the architecture.

The Chat uses a connection to a FMS server, since i may extend my app with video and so on, i should separate the connection Handling form the ChatProxy.

Is a FmsConnectionService is a good way to this ? So far i did it this way:
The service is a singleton since i need only one connection, and it can send Notifications too.
(is it still a service then or should it be a proxy) ?

ackage org.edorado.edoboard.model.service
import org.puremvc.as3.interfaces.INotifier;
import org.puremvc.as3.patterns.observer.Notifier;

import flash.net.NetConnection;
import flash.events.NetStatusEvent;

    import org.edorado.edoboard.ApplicationFacade;
* FMS connection Singleton

public class FMSConnectionService extends Notifier implements INotifier
private var nc:NetConnection;

private static var instance:FMSConnectionService;

public function FMSConnectionService() {

         * Create the required connection object and do any configuration
        private function setupConnection():void {
            //A NetConnection is the pipe between Flex/Air and Flash Media Server
            nc = new NetConnection();
            //The NetStatusEvent is a dispatched status event for
            //NetConnection, NetStream, or SharedObject.
            //We want to know if the NetConnection has properly connected
            nc.addEventListener( NetStatusEvent.NET_STATUS, handlerOnNetStatus );
        // Singleton Service
        public static function getInstance() : FMSConnectionService {
            if ( instance == null ) instance = new FMSConnectionService( );
                return instance as FMSConnectionService;
         * Connect to the given URL
         public function connect(url:String):void {
         * Disconnect  the net connection
         public function close():void {
        public function getNetConnection():NetConnection {
            return nc;
         *  This is the handler function of the NetStatusEvent for the NetConnection. 
         *  Here is where we can find out the connection status and send notifications 
         *  based on each result.
         private function handlerOnNetStatus( event:NetStatusEvent ):void {
            var info:Object = event.info;
            //Checking the event.info.code for the current NetConnection status string
                //code == NetConnection.Connect.Success when Netconnection has successfully
                case "NetConnection.Connect.Success":
                    sendNotification(ApplicationFacade.CONNECT_SUCCESS, info.code);
                //code == NetConnection.Connect.Rejected when Netconnection did
                //not have permission to access the application.   
                case "NetConnection.Connect.Rejected":
                    sendNotification(ApplicationFacade.CONNECT_REJECTED, info.code);
                //code == NetConnection.Connect.Failed when Netconnection has failed to connect
                //either because your network connection is down or the server address doesn't exist.   
                case "NetConnection.Connect.Failed":
                    sendNotification(ApplicationFacade.CONNECT_FAILED, info.code);
                //code == NetConnection.Connect.Closed when Netconnection has been closed successfully.         
                case "NetConnection.Connect.Closed":
                    sendNotification(ApplicationFacade.CONNECT_CLOSED, info.code);

My chatProxy has a public  fMSConnectionService  variable that i can use to get the netConnection object.

Now, if i want to check for the success of my connection before i register the ChatProxy and ChatMediator. I would need to access the Service and call connect() from my ApplicationMediator and that is BAD from what i read, service should only be used by proxies.

What would be a good solution then ?

Thanks for the enlightment.

Title: Re: Concept Question, proxy and vo for a chat
Post by: puremvc on October 14, 2008, 06:25:12
I would make the FmsConnectionService a Proxy, register it and later, have the ChatProxy (and any other Proxy that ends up needing the connection) retrieve the ConnectionProxy  in onRegister and check if the connection is valid. If not they can thow error, or ask the ConnectionProxy to retry, or unRegister themselves, as needed.

This is a Proxy acting as a Delegate for services.