Skip to main content

API & Config

Create P2PEngineMedia instance

var engine = new P2PEngineMedia(p2pConfig);

Create a new P2PEngineMedia instance.

If p2pConfig is specified, then the default options (shown below) will be overridden.

logLevelstring|boolean'error'Print log level(warn, error, none,false=none, true=warn).
tokenstringundefinedToken is used to summarize and display multi domain name data on the console. In addition, token is required while customizing channelId.
trackerZonestring'eu'The country code name of the tracker server address('eu', 'hk', 'us').
pieceLengthnumber512 * 1024Length in bytes of every piece but the last one.
mediaElemHTMLMediaElement|stringundefinedSpecify the ID or element object of the video/audio tag, default value is the first video/audio element in the document tree.
p2pEnabledbooleantrueEnable or disable p2p engine.
webRTCConfigObject{}A Configuration dictionary providing options to configure WebRTC connections.
tagstring''User customized tag.
swFilestring'./sw.js'The file name and path of ServiceWorker.
swScopestring'./'The default scope is the location of the ServiceWorker file, and extends to all directories below. So if sw.js is located in the root directory, the ServiceWorker will control requests from all files at this domain.
showSloganbooleanfalseDisplay slogan of cdnbye on console.
diskCacheLimitObject{"pc": 2500 1024 1024, "mobile": 1500 1024 1024}The max size of binary data that can be stored in the disk cache.
minBufferCountnumber0Minimum buffer count of pieces. If buffer pieces is/become less than this value, a new piece will be loaded.

P2PEngineMedia API

P2PEngineMedia.version (static)

Get the version of P2PEngineMedia.

P2PEngineMedia.protocolVersion (static)

Get the version of P2P protocol.

P2PEngineMedia.isSupported() (static)

Returns true if both WebRTC datachannel and ServiceWorker are supported by the browser.

P2PEngineMedia.isWebRTCSupported() (static)

Returns true if WebRTC datachannel is supported by the browser.

P2PEngineMedia.isSeviceWorkerSupported() (static)

Returns true if ServiceWorker is supported by the browser.


Get the promise of proxied playback url.


Start p2p.


Stop p2p, free used resources.


Restart p2p 。

P2PEngineMedia Events

engine.on('peerId', function (peerId) {})

Emitted when the peer Id of this client is obtained from server.

engine.on('peers', function (peers) {})

Emitted when successfully connected with new peer.

engine.on('stats', function (stats) {})

Emitted when data is downloaded/uploaded.
stats.totalHTTPDownloaded: total data downloaded by HTTP(KB).
stats.totalP2PDownloaded: total data downloaded by P2P(KB).
stats.totalP2PUploaded: total data uploaded by P2P(KB).
stats.p2pDownloadSpeed: p2p download speed(KB/s).

engine.on('serverConnected', function (connected) {})

Emitted when websocket is opened/closed.

engine.on('exception', function (e) {})

Emitted when exception occured.
e.code: Exception identifier(TRACKER_EXPT SIGNAL_EXPT)
e.message: Exception message
e.stack: Exception stack

Get p2p information from p2pConfig

p2pConfig: {
getStats: function (totalP2PDownloaded, totalP2PUploaded, totalHTTPDownloaded, p2pDownloadSpeed) {
// get the downloading statistics
getPeerId: function (peerId) {
// get peer Id
getPeersInfo: function (peers) {
// get peers information

MediaProxy.version (static)

Get the version of MediaProxy.

Advanced Usage

Dynamic MP4/MP3 Path Support

The channelId is an identifier used by our backend to match peers that are watching the same content. It is an optional parameter, and by default, we generate channelId from the content URL by removing any query parameters and protocol from it. Some mp4/mp3 urls play the same live/vod but have different paths on them. For example, and In this case, you can format a common channelId for them.

// Set token in p2pConfig before setting channelId! Connectivity with other platform should have the same token.
p2pConfig: {
token: YOUR_TOKEN,
channelId: function (url) {
const videoId = extractVideoIdFromUrl(url); // make a channelId by removing the different part which is defined by yourself
return videoId;
// channelId: VIDEO_ID // for fixed channel id

Interconnect with other platform should ensure that both have the same token and channelId.