NetStream class



Opens a one-way streaming connection between Flash Media Interactive Server and a remote Flash Media Interactive Server through a NetConnection object. A NetStream object is a channel inside a NetConnection object; call NetStream.publish() to publish data over this channel. Unlike a client-side NetStream object, a server-side NetStream object can only publish data; it cannot subscribe to a publishing stream or play a recorded stream.

Use the NetStream class to scale live broadcasting applications to support more clients. Flash Media Interactive Server can support only a certain number of subscribing clients. To increase that number, you can use the NetStream class to move traffic to remote servers while still maintaining only one client-to-server connection.

The following steps describe the workflow for publishing a stream to a remote Flash Media Interactive Server:

  1. Call the NetConnection constructor, nc = new NetConnection, to create a NetConnection object.

  2. Call nc.connect("rtmp://serverName/appName/appInstanceName") to connect to an application on a remote Flash Media Interactive Server.

Note: You cannot use RTMPT, RTMPE, or RTMPTE when connecting to a remote server.
  1. Call the NetStream constructor, ns = new NetStream(nc), to create a stream over the connection.

  2. Call ns.publish("myStream") to give the stream a unique name and send data over the stream to the remote server. You can also record the data as you publish it, so that users can play it back later.

  3. Clients that subscribe to this stream connect to the same application on the remote server (in a client-side script), NetConnection.connect("rtmp://serverName/appName/appInstanceName"), and then call NetStream.play("myStream") with the same stream name.

Availability

Flash Media Interactive Server 3 and Flash Media Development Server 3

Property summary

Property

Description

NetStream.bufferTime

Read-only; indicates the number of seconds assigned to the buffer by the NetStream.setBufferTime() method.

NetStream.time

Read-only; indicates the number of seconds the stream has been publishing.

Method summary

Method

Description

NetStream.attach()

Attaches a data source to the NetStream object.

NetStream.publish()

Publishes a stream to a remote server.

NetStream.send()

Broadcasts a data message over a stream.

NetStream.setBufferTime()

Sets the size of the outgoing buffer in seconds.

Event handler summary

Event handler

Description

NetStream.onStatus()

Invoked every time a status change or error occurs in a NetStream Object.

NetStream class constructor

ns = new NetStream(connection)

Creates a stream that can be used for publishing (sending) data through the specified NetConnection object. However, you can create multiple streams that run simultaneously over the same connection.

Availability

Flash Media Interactive Server 3 and Flash Media Development Server 3

Parameters

connection
A NetConnection object.

Returns

A NetStream object if successful; otherwise, null.

Example

nc = new NetConnection(); 
nc.connect("rtmp://xyz.com/myApp"); 
ns = new NetStream(nc);

NetStream.attach()

ns.attach(stream)

Attaches a data source to the NetStream object.

Availability

Flash Media Interactive Server 3 and Flash Media Development Server 3

Parameters

stream
A Stream object. If you pass false, the attached Stream object detaches from the NetStream object.

Returns

A boolean value. If the attached object is a valid data source, true; otherwise, false.

Example

myStream = Stream.get("foo"); 
ns = new NetStream(nc); 
ns.attach(myStream);

NetStream.bufferTime

ns.bufferTime

Read-only; indicates the number of seconds assigned to the buffer by the NetStream.setBufferTime() method.

Availability

Flash Media Interactive Server 3 and Flash Media Development Server 3

NetStream.onStatus()

ns.onStatus = function(infoObject){})

Invoked every time a status change or error occurs in a NetStream object. The remote server can accept or reject a call to NetStream.publish().

Availability

Flash Media Interactive Server 3 and Flash Media Development Server 3

Parameters

infoObject
An Object with code and level properties that provide information about the status of a NetStream call. Both properties are strings.

Code property

Level property

Description

NetStream.Publish.Start

status

An attempt to publish was successful.

NetStream.Publish.BadName

error

An attempt was made to publish to a stream that is already being published by someone else.

NetStream.Unpublish.Success

status

An attempt to stop publishing a stream was successful.

NetStream.Record.Start

status

Recording was started.

NetStream.Record.Stop

status

Recording was stopped.

NetStream.Record.NoAccess

status

An attempt was made to record a read-only stream.

NetStream.Record.Failed

error

An attempt to record a stream failed.

NetStream.Record.DiskQuotaExceeded

error

An attempt to record a stream failed because the disk quota wasexceeded. For more information, see Stream.record().

Example

ns = new NetStream(nc); 
ns.onStatus = function(info){ 
    if (info.code == "NetStream.Publish.Start"){ 
        trace("It is now publishing"); 
    } 
ns.publish("foo", "live"); 
}

NetStream.publish()

ns.publish(name, howToPublish)

Publishes a stream to a remote server. Check the status in the NetStream.onStatus() handler to make sure that the remote server has accepted the publisher. If the stream has been published by another client, the publish() call can fail when it reaches the remote server. In this case, the remote server sends a status message of "NetStream.Publish.BadName" to the NetStream.onStatus() method.

Availability

Flash Media Interactive Server 3 and Flash Media Development Server 3

Parameters

name
A string identifying the stream to publish. If you pass false, the stream stops publishing. Use the following syntax:

File format

Syntax

FLV

ns.publish("filename")

MP3

ns.publish("mp3:filename")

ns.publish("id3:filename")

MP4

ns.publish("mp4:filename")

ns.publish("mp4:filename.mp4")

ns.publish("mp4:filename.f4v")

howToPublish
An optional string specifying how to publish the stream. Valid values are "record", "append", and "live". The default value is "live". If you omit this parameter or pass "live", live data is published but not recorded. If a file with this name already exists on the remote server, it is deleted.
Note: If the file is read-only, live data is published and the file is not deleted.

If you pass "record", the stream is published and the data is recorded to a new file. If the file exists, it is overwritten. If you pass "append", the stream is published and the data is appended to the existing stream specified by name. If no file is found, it is created.

The server stores recorded files in the streams subfolder of the application’s folder, for example, RootInstall/applications/sampleApplication/_definst_/streams. The recorded file has the filename passed in the name parameter. For example, NetStream.publish ("mp4:streamname.f4v", "record") creates the file streamname.f4v.

Example

application.onPublish = function(client, myStream){ 
    nc = new NetConnection(); 
    nc.connect("rtmp://example.com/myApp"); 
    ns = new NetStream(nc); 
    ns.attach(myStream); 
    ns.publish(myStream.name, "live"); 
};

The following example shows how to record an F4V file on the remote server.

application.onPublish = function(client, myStream){ 
    nc = new NetConnection(); 
    nc.connect("rtmp://example.com/myApp"); 
    ns = new NetStream(nc); 
    ns.attach(myStream); 
    ns.publish("mp4:" + myStream.name, "record"); 
};

NetStream.send()

ns.send(handlerName, [p1, ..., pN])

Broadcasts a data message over a stream.

Availability

Flash Media Interactive Server 3 and Flash Media Development Server 3

Parameters

handlerName
A string that identifies the name of the handler to receive the message.

p1, ..., pN
Optional parameters of any type. They are serialized and sent over the connection. The receiving handler receives them in the same order.

Returns

A boolean value; true if the data message is dispatched; otherwise, false.

Example

The following client-side code broadcasts the message "Hello world" to the foo handler function on each client that is connected to myApp:

nc = new NetConnection(); 
nc.connect("rtmp://xyz.com/myApp"); 
ns = new NetStream(nc); 
ns.send("foo", "Hello world");

NetStream.setBufferTime()

ns.setBufferTime(bufferTime)

Sets the size of the outgoing buffer in seconds. If publishing, it controls the buffer in the local server.

Availability

Flash Media Interactive Server 3 and Flash Media Development Server 3

Parameters

bufferTime
A number indicating the size of the outgoing buffer in seconds.

Example

nc = new NetConnection(); 
nc.connect("rtmp://xyz.com/myApp"); 
ns = new NetStream(nc); 
ns.setBufferTime(2);

NetStream.time

ns.time

Read-only; indicates the number of seconds the stream has been publishing. This is a good indication of whether data is flowing from the source that has been set in a call to the NetStream.attach() method.

Availability

Flash Media Interactive Server 3 and Flash Media Development Server 3