Stream from multiple sources for better reliability
Here we have configured three HTTP streaming sources into a cluster. The URLs of the first two hosts are wrong on purpose to demonstrate failing hosts. The streaming will begin from the third host and you can see what is happening in the box below the player.
Features
- Fallback to another host when the primary host is not available.
- Load balancing by distributing requests evenly between configured hosts.
- Supports HTTP, RTMP and pseudostreaming.
- Host failures are remembered and new connection attempts are only attempted after a preconfigured timeout.
Example
The configuration for the above demo is shown below. It listens to the
onConnect and onConnectFailed events to show info of the connection
attempts.
// a global variable that references our info box
var info = document.getElementById("info");
flowplayer("player", "http://releases.flowplayer.org/swf/flowplayer-3.2.11.swf", {
log: { level: 'debug', filter: 'org.flowplayer.cluster.*' },
plugins: {
// cluster plugin configuration
cluster: {
url: "flowplayer.cluster-3.2.8.swf",
// our hosts on the cluster
hosts: [
// first two hosts will fail
'http://invalid.flowplayer.org/video',
'http://nonexistent.flowplayer.org/video',
// this one will answer (if the server is up)
'http://pseudo01.hddn.com/vod/demo.flowplayervod'
],
// callback method for connection attempt
onConnect: function(host, index) {
info.innerHTML += "attempting to connect: " + host + "<br />";
},
// callback method for connection failure
onConnectFailed: function(host, index) {
info.innerHTML += "connection failed to: " + host + "<br />";
}
}
},
// use our cluster plugin as a "URL resolver"
clip: {
urlResolvers: 'cluster',
onStart: function(clip) {
info.innerHTML += "clip started: " + clip.url;
}
}
});
Configuration
Here is a list of the configuration options:
| property / datatype | Default | Description |
|---|---|---|
|
|
|
The hosts that are part of the cluster. These should be base URLs for each host to be used together with the clip's URL. The complete URL will be formed by concatenating the host URL and the clip's URL. |
|
|
|
Connection timeout for a single connection attempt, given in milliseconds. After this timeout has been reached, the connection attempt is considered a failure and the next host in the cluster is evaluated. |
|
|
|
Number of times to loop through all the hosts in the cluster before giving up. |
|
|
|
The expiry time (in milliseconds) to wait before a failed server is attempted again. The failure timestamp is stored in the client browser and a new connection attempt is only attempted after this time has expired. |
|
|
|
When this is set to |
Load balancing
The plugin can be set to randomly pick hosts from the cluster. This allows for
simple load balancing completely controlled on the client side. This is achieved
with the loadBalance property. Click on the splash image to see an example. It
will randomly pick a host from the two alternatives.
Clustering events appear here
We have two different video servers: pseudo01.hddn.com is hosted by HDDN and
stream.flowplayer.org by Amazon. These two are accessed randomly so that
in the end both servers will be loaded evenly. The load balancing is enabled with
the loadBalance property.
// a global variable that references our info box
var infoBox = document.getElementById("balanceInfo");
flowplayer("balancer", "http://releases.flowplayer.org/swf/flowplayer-3.2.11.swf", {
// configuration for the clustering plugin
plugins: {
cluster: {
url: "http://releases.flowplayer.org/swf/flowplayer.cluster-3.2.8.swf",
// two working hosts
hosts: [
'http://pseudo01.hddn.com/vod/demo.flowplayervod',
'http://stream.flowplayer.org'
],
// that are load balanced (accessed randomly)
loadBalance: true,
// callback method that updates our info box
onConnect: function(host, index) {
infoBox.innerHTML += "attempting to connect to: " + host + "<br />";
}
}
},
clip: {
// our clip uses clustering plugin
urlResolvers: 'cluster',
onStart: function(clip) {
infoBox.innerHTML += "started clip: " + clip.url;
}
}
});
It's also possible to work with a combination of client-side and server-side load balancing. The server-side load balancing is usually done using a load balancer running in front of the RTMP or HTTP streaming servers.
Clustering RTMP servers
A cluster of RTMP servers is configured like this:
flowplayer("player", "http://releases.flowplayer.org/swf/flowplayer-3.2.11.swf", {
clip: {
// for FMS we don't use the file extension here
url: 'Extremists',
provider: 'rtmp',
// the clustering plugin is configured as a connectionProvider
// when used with RTMP
connectionProvider: 'clustering'
},
plugins: {
// we don't need the netConnectionUrl here because it is
// taken from the hosts array of the cluster
rtmp: {
url: "flowplayer.rtmp-3.2.10.swf"
},
clustering: {
url: "http://releases.flowplayer.org/swf/flowplayer.cluster-3.2.8.swf",
netConnectionUrl: 'rtmp://cyzy7r959.rtmphost.com/flowplayer',
// Our hosts array contains the netConnectionUrls to the servers
hosts: [
{host:'rtmp://cyzy7r959.badhost1.com/flowplayer'},
{host:'rtmp://cyzy7r959.badhost2.com/flowplayer'},
{host:'rtmp://cyzy7r959.rtmphost.com/flowplayer'}
]
}
}
});
JavaScript API
Methods
| Method | Returns | Description |
|---|---|---|
|
setLoadBalancing(value) |
Enables/disables load balancing. When enabled, the plugin randomly
picks hosts from the cluster.
For example: |
Events
| Event | When does it fire? |
|---|---|
|
onConnect() |
Fires when the plugin starts a new connection attempt. The callback is fed with two arguments:
|
|
onConnectFailed() |
Fires when a connection attempt has failed. The callback is fed with two arguments:
|
|
onFailed() |
Fires when all hosts in the cluster have failed. See also the connectCount option, that specifies how many times the hosts are evaluated before failing. |
Download
| flowplayer.cluster-3.2.8.swf | just the working flash file to get you going |
| flowplayer.cluster-3.2.8.zip | working flash file (swf) + README.txt and LICENSE.txt |
| flowplayer.cluster-3.2.8-src.zip | source code |
Please right-click and choose "Save link as..." (or similar)
See the version history for this tool.
Found a bug?
If you encounter problems in this script, please send a bug report to the bug reporting forum. If you have a problematic page, including a direct URL to that page is by far the most effective way of helping us to find a bug.