Server as Peer
Introduction
This program makes server as a peer (Super Peer) to serve clients that cannot connect to other peers. The master/worker architecture is adopted. The master is responsible for creating and scheduling worker processes, and the worker process is responsible for executing "seeding". Each worker can only seed for one channel, and a channel can be seeded by multiple workers simultaneously.
Server Spec
Each worker process can serve up to 200 viewers, requires approximately one CPU core, 400MB of memory, and 200Mbps of bandwidth, it also requires sufficient disk space to cache content for VOD.
Linux OS Requirement
- Ubuntu 20 or newer
- CentOS 8 or newer
SDK Version Requirement
Web HLS SDK version >= 2.7.0
Android SDK version >= 3.4.0
Install
Install Automatically
The script to start super peer with listening port 8080 is as follows:
wget -qN https://cdn.swarmcloud.net/super-peer.sh && bash super-peer.sh --port 8080
Install Manually
Install Node.js
Ignore this step if you have installed node.js.
wget https://npmmirror.com/mirrors/node/v18.15.0/node-v18.15.0-linux-x64.tar.xz
tar -xvf node-v18.15.0-linux-x64.tar.xz
sudo mkdir -p /usr/local/nodejs
sudo mv node-v18.15.0-linux-x64/* /usr/local/nodejs/
sudo ln -s /usr/local/nodejs/bin/node /usr/local/bin
sudo ln -s /usr/local/nodejs/bin/npm /usr/local/bin
Download super-peer.zip
wget https://cdn.swarmcloud.net/super-peer.zip
unzip super-peer.zip
Start Super Peer
The script to start super peer with listening port 8080 is as follows:
cd super-peer && echo listenPort=8080 >> .env
pm2 is a Node.js application process manager that is used for keeping super peer alive forever.
npm i -g pm2
sudo ln -s /usr/local/nodejs/bin/pm2 /usr/local/bin
pm2 start index.js -n super-peer
Register Super Peer in Dashboard
Log in dashboard, click on "P2P Setting" -> "Server as Peer" -> "+Super Peer", input the server URL(http://ip:port) and bandwidth, then click "Confirm". If the server status shows "Stopped", it means that the server cannot be connected. Please ensure that the firewall configuration is correct.
For live streaming, super peer can only serve clients within a straight distance of 7000 kilometers. Please choose a server that is as close to the user as possible to deploy.
Signal Configuration
If your application has configured signal address on the console, you need to configure the same signal address for the super peer domain.
Seeding Strategy
By default, the tracker server will automatically seed based on the current hot channel rankings. You can turn off automatic seeding and do it manually.
Monitor
Click on the monitor button of the corresponding server on the page to view information such as the number of current connected peers, uploaded traffic in KB, and system resource usage for each worker process. You can also restart or kill a specific worker process.
Access Token (Optional)
You can set an access token before starting the program:
echo 'accessToken=ACCESS_TOKEN' >> .env
ACCESS_TOKEN is the self-defined string, which should be same as the one registered in dashboard.
Signaling Address (Optional)
In addition to configuring the signaling address on the console, it can also be configured through the .env file to use different signaling addresses in different regions:
echo 'signal1=wss://example.com' >> .env
# echo 'signal2=wss://example2.com' >> .env
signal1 is the primary signaling address, and signal2 is the backup signaling address.
Auto Startup (Optional)
Start the program then run:
pm2 save && pm2 startup
Update Super Peer (Optional)
Replace the file with new version, then restart the process:
wget -N https://cdn.swarmcloud.net/super-peer.zip && unzip -o super-peer.zip
pm2 restart super-peer
pm2 save
or simply run:
wget -qN https://cdn.swarmcloud.net/super-peer.sh && bash super-peer.sh
Restful API
HTTP Header
Name | Type | Description | Required |
---|---|---|---|
X-ACCESS-TOKEN | string | 访问token,需要与超级节点启动时设置的一致 | 否 |
Ping
Check connectivity while setting bandwidth or worker count
POST /ping
POST Body
Name | Type | Description | Required |
---|---|---|---|
bandwidth | int | bandwidth in Mbps | No |
workers | int | Force the number of worker processes | No |
Response
Name | Type | Description | |
---|---|---|---|
workers | int | Current number of workers processes |
Seed
Send seeding request
POST /seed
POST Body
Name | Type | Description | Required |
---|---|---|---|
list | array | The list of seeding information | Yes |
Body Example:
{
"list": [
{
"token": user token
"channel": channel ID(Base64 encoded)
"trackerZone": cn, hk, us
"live": live streaming or vod
"signal1": primary signal address
"signal2": backup signal address
"priority": priority of seeding
"keepAlive": always alive
"noUseDiskCache": does not use disk cache for vod
"maxConnections": maximum p2p connections, default is 80
"label": customized super peer label
}
]
}
Response
Status: 200
Stats
Request all real-time statistics
GET /stats
Request statistics for one worker
GET /stats/:pid
Where pid is the process id of the worker
Request statistics for one channelId
GET /stats/:channelId
Where channelId is the base64 encoded channel id
Stop all workers
POST /stop_all
Stop one worker
POST /stop/:pid
Restart all workers
POST /restart_all
Restart one worker
POST /restart/:pid