Publishing from server to server

About multipoint publishing

Multipoint publishing allows clients to publish to servers with only one client-to-server connection. This feature enables you to build large-scale live broadcasting applications, even with servers or subscribers in different geographic locations.

View full size graphic
Using multipoint publishing to publish content from server to server, even across geographic boundaries

A.
Live Video

B.
Server 1 (New York City)

C.
Server 2 (Chicago) and Server 3 (Los Angeles)

D.
Users

The application flow that corresponds to this illustration works like as follows:

  1. A client connects to an application on Server 1 in New York City and calls NetStream.publish() to publish a live stream. This client can be a custom Flash Player or AIR application or Flash Media Live Encoder.

  2. The server-side script on Server 1 receives an application.onPublish() event with the name of the published stream.

  3. The application.onPublish() handler creates a NetStream object and calls NetStream.publish() to republish the live stream to Server 2 (Chicago) and Server 3 (Los Angeles).

  4. Subscribers connecting to Server 2 and Server 3 receive the same live stream.

  5. The applications receive application.onUnpublish() events when the clients stops publishing.

Example: Multipoint publishing

In this example, the client captures, encodes, and publishes the stream to the server. You can also use Flash Media Live Encoder for the same purpose.

Note: To test this code, create a RootInstall/applications/livestreams folder on the server. Open the Administration Console and create an instance of the livestreams application. Click Live Logs to see the server-side trace() statements as the application runs. Open the RootInstall/documentation/samples/livestreams/LiveStreams.swf file to connect to the application.
  1. In a client-side script, call the NetStream.publish() method to publish a live stream:

    ns.publish("localnews", "live");
    Note: To use Flash Media Live Encoder as a publishing client, enter the FMS URL rtmp://localhost/livestreams and the Stream localnews.
  2. In the server-side main.asc file, define an application.onPublish() event handler. This handler accepts the stream name that was published from the client, connects to the remote server, and republishes the stream to the remote server. (In this example, the remove server is another instance of the same application).
    // Called when the client publishes 
    application.onPublish = function(client, myStream) { 
        trace(myStream.name + " is publishing into application " + application.name); 
        // This is an example of using the multi-point publish feature to republish 
        // streams to another application instance on the local server. 
        if (application.name == "livestreams/_definst_"){ 
            trace("Republishing the stream into livestreams/anotherinstance"); 
            nc = new NetConnection(); 
            nc.connect( "rtmp://localhost/livestreams/anotherinstance" ); 
            ns = new NetStream(nc); 
            // called when the server NetStream object has a status 
            ns.onStatus = function(info) { 
                trace("Stream Status: " + info.code) 
                if (info.code == "NetStream.Publish.Start") { 
                    trace("The stream is now publishing"); 
                }           
            } 
            ns.setBufferTime(2); 
            ns.attach(myStream); 
            ns.publish( myStream.name, "live" ); 
        } 
    }

    Calling NetStream.publish() publishes the stream from your server to the remote server.

  3. In the main.asc file, handle events that occur on the NetStream object you used to publish from your server to the remote server:

    ns.onStatus = function(info) { 
        trace("Stream Status: " + info.code) 
        if (info.code == "NetStream.Publish.Start") { 
            trace("The stream is now publishing"); 
        }           
    }

    The server-side NetStream.publish() method triggers a NetStatus event with a NetStream.Publish.Start code, just like the client-side NetStream.publish() method.

  4. Define what happens when the client stops publishing:

    application.onUnpublish = function( client, myStream ) { 
        trace(myStream.name + " is unpublishing"  ); 
    }