Skip to content
Paul Gregoire edited this page Jul 5, 2016 · 1 revision

Before jumping into Shared Objects on Red5, you may want to review the Adobe documentation here if at the very least to provide you with the terminology which will be used. Also to be clear, we are mainly concerned with Remote Shared Objects and not regular Local Shared Objects.

What is a Shared Object

A SharedObject lets you store data on the server and share data between multiple client applications in real time. Shared objects can be temporary, or they can persist on the server after an application has closed; you can consider shared objects as real-time data transfer devices.

Accessing a Shared Object on the Client

To access a Remote Shared Object from a client, we must first establish a connection to our server and once that is successful, we set our client to observe sync events.

    private var nc:NetConnection;
    private var so:SharedObject;

    // connect to the server
    private function connectToServer():void {
        nc = new NetConnection();
        nc.client = this;
        nc.addEventListener(NetStatusEvent.NET_STATUS, onNetConnectionStatus);
        nc.connect("rtmp://127.0.0.1/myapp/_definst_", "dummy");
    }

    // handle status events
    private function onNetConnectionStatus(e:NetStatusEvent):void {
        if (e.info.code == "NetConnection.Connect.Success") {
            so = SharedObject.getRemote("foo", nc.uri);
            so.addEventListener(SyncEvent.SYNC, onSync);
            so.connect(nc);
        }
    }

    private function onSync(event:SyncEvent):void {
        for (var cl:int = 0; cl < event.changeList.length; cl++) {
            var changeObj:Object = event.changeList[cl]; 
            switch (changeObj.code) {
                case "change":
                    // changes to attributeMap in foo, will show up here
                    if (changeObj.name === "attributeMap" && sharedObj.data[changeObj.name]) {
                        trace("Key1: " + sharedObj.data["attributeMap"].key1);                
                        trace("Key2: " + sharedObj.data["attributeMap"].key2);
                    }
                    break;
            }
        }
    }

To call the method on the server which updates the map in the Shared Object, you could do the following in your client code:

    private function updateMap(e:MouseEvent):void {
        // pass an object as parameter #3 or null
        nc.call("updateMap", null, null);
    }

Accessing a Shared Object on the Server-side

The method below shows you how to access and modify a Shared Object on the server-side via RPC. This example method would be placed in your Application Adapter class and is called from a connected Flash Player or other RTMP client.

    // Update a Shared Object map or create new if one is not passed
    public void updateMap(Map<String, Object> map) {
        // get the current connection making the request
        IConnection conn = Red5.getConnectionLocal();
        // get the shared object by name in the connections scope
        ISharedObject so = getSharedObject(conn.getScope(), "foo");
        // if no map is passed as a parameter, create one
        if (map == null) {
            map = new HashMap<>();
        }
        // put some key value pairs in the map
        map.put("key1", RandomStringUtils.randomAscii(6));
        map.put("key2", RandomStringUtils.randomAscii(6));
        // put the map into the shared object with the name "attributeMap"
        so.setAttribute("attributeMap", map);
        // mark the shared object as "dirty", meaning it has been modified
        so.setDirty(true);
    }

All clients connected to Shared Object foo would receive a change event once the method completes.