Quick Tip: Working with NetStream

While working on my brand new P2P project I have come to some issues and solutions, that may help you with your projects. First thing, the NetStatusEvent codes. You may expect that codes are dispatched by the instance of the class they beigins with (e.g. “NetStream.Connect.Success” dispatched by NetStream), but in fact, this does not happen (“NetStream.Connect.Success” is dispatched by NetConnection). I bellieve its not a bug but a feature by flash player developer team. To make this work as expected you may do the following:

netConnection.addEventListener(type, onNetStatus);
function onNetConnectionNetStatus(event:NetStatusEvent):void
		case "NetStream.Connect.Success":

This way your NetStream dispatches the same NetStatusEvent into itself. You may now wonder why am I doing it? There are 2 reasons for that:

  • This way I can easily handle multiple NetStreams (in multiple objects)
  • This event may be used for attaching camera/microphone into your NetStream (I guess due to peer-assisted security restrictions).
netGroup.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
netStream.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);

function onNetStatus(event:NetStatusEvent):void
		case "NetStream.Connect.Success":
		case "NetGroup.Neighbor.Connect":
		case "NetGroup.Neighbor.Disconnect":

attaching camera / microphone after NetStream is connected, prevents you from follofing:

var camera:Camera = Camera.getCamera();
var netStream:NetStream = new NetStream(netConnection, 

…will result in runtime error:

Error #2154: The NetStream Object is invalid. This may be due to a failed NetConnection.


Getting this annoying, undocumented error code?

    private function onConnect():void
        var netGroup:NetGroup = new NetGroup(netConnection, groupspec);
        netGroup.addEventListener(type, onNetStatus);
    // dispatches NetStatusEvent with code NetGroup.Connect.Closed

… workaround:

    private var var netGroup:NetGroup; // using class variable!

    private function onConnect():void
        netGroup = new NetGroup(netConnection, groupspec);
        netGroup.addEventListener(type, onNetStatus);

This seems like flash player bug. I guess this bug has something to do with reference to a NetGroup instance. If you use function variable, this get cleared after function finishes even with attached listener (guess), while class variable reference remains.

6 comments so far

  1. Daniel October 12, 2010 09:59

    Hello friend.
    I’m just developing my first p2p app and I came up with some of your issues too.
    The “NetStream.Connect.Success” event was driving me crazy.
    Since I use only one instance of my netConnection (a singleton) for the entire app,
    I was loosing control of my listeners because of that “feature”.
    And your workaround saved me. Thank you.
    But tell me, how do you manage your netConnections? Do you use a singleton for the entire app too or do you create a new netConnection for each component that needs it?

  2. Jozef Chúťka October 12, 2010 10:05

    Hi Daniel, I usualy use only one NetConnection instance for the whole app. I believe it is enough, while you can attach as much NetStreams as you wish …. it may be singleton, sure.

  3. kyle November 18, 2010 08:55

    Have you had success with this working with NetStream.DIRECT_CONNECTION…
    i can’t get the netconnection object to fire the NetStream.Connect.Success event..

    When I use a singleton for the NetConnection it doesn’t work..
    if i create a brand new NetConnection object before i create my new NetStream it works but that is not helpful cause then i have to fetch brand new nearID’s for all connected peers in the group inorder for them to talk to eachother…

    can’t find any information on this but i think its a bug.


  4. Jozef Chúťka November 18, 2010 16:13

    Hi kyle,
    I did not have issues having NetStream.Connect.Success dispatched over NetConnection. Just make sure you define NetStream instance that is referenced somewhere. See the section “NetGroup.Connect.Closed”, the same thing happens to NetStream if you create function variable and you loose reference to it – it gets garbaged.

  5. novaemp February 20, 2014 05:29

    Currently , I am running a video file from wowza server.
    It is well working now.
    But, after video is be play once and passed for several second , in flash player screen will be show – “error: error #2154”
    what problem?
    I wish your advice asap.


  6. Jozef Chúťka February 20, 2014 09:35

    hi novaemp,
    to resolve error 2154, have a look at http://forums.adobe.com/thread/581550

Leave a comment

Please be polite and on topic. Your e-mail will never be published.