{"id":16889914,"url":"https://github.com/t-mullen/peer-tree","last_synced_at":"2025-04-11T13:08:59.441Z","repository":{"id":137583700,"uuid":"97340689","full_name":"t-mullen/peer-tree","owner":"t-mullen","description":"A k-tree one-to-many network for WebRTC.","archived":false,"fork":false,"pushed_at":"2019-12-19T06:49:34.000Z","size":170,"stargazers_count":15,"open_issues_count":1,"forks_count":1,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-03-25T09:21:33.602Z","etag":null,"topics":["broadcast","p2p","webrtc"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/t-mullen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-07-15T20:29:23.000Z","updated_at":"2025-02-11T10:30:23.000Z","dependencies_parsed_at":null,"dependency_job_id":"cbfa2f53-c849-4074-b674-58d8a346cfec","html_url":"https://github.com/t-mullen/peer-tree","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/t-mullen%2Fpeer-tree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/t-mullen%2Fpeer-tree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/t-mullen%2Fpeer-tree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/t-mullen%2Fpeer-tree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/t-mullen","download_url":"https://codeload.github.com/t-mullen/peer-tree/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248404773,"owners_count":21097824,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["broadcast","p2p","webrtc"],"created_at":"2024-10-13T16:59:47.726Z","updated_at":"2025-04-11T13:08:59.413Z","avatar_url":"https://github.com/t-mullen.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# peer-tree\n[![Standard - JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)\n\n**peer-tree** connects an unlimited number of WebRTC peers in a k-tree (like a binary tree, but with k downstream connections instead of just 2). Peers can forward data and video streams down to the peers below them. This allows one-to-many broadcast to a huge number of peers with extremely low latency.\n\n## Install\n\nServer:\n```\nnpm install peer-tree-server\n```\n\nClient (with Browserify):  \n```\nnpm install peer-tree-client\n```\n\n*A standalone client build is available in **dist** as well.*\n\n## Usage\n\nServer:\n\n```javascript\nvar io = require('socket.io')() // A socket.io instance for signalling\nvar PeerTreeServer = require('peer-tree-server')\n\nvar treeServer = new PeerTreeServer(io, {\n  k: 2 // k is the maximum number of downstream connections per peer,\n  k1: 10 // k1 is the maximum number of downstream connections for the source\n})\n```\n\nClient: \n\n```javascript\nvar socket = require('socket.io-client')() // A socket.io-client instance for signalling\nvar treeClient = new PeerTreeClient(socket)\n\n// If you want to create a new tree (and become the source)\ntreeClient.create()\ntreeClient.on('discover', (treeID) =\u003e {\n  // The tree has been created with the returned ID\n})\ntreeClient.on('downstreamPeer', (peer) =\u003e {\n  // pipe our data to every downstream peer\n  readableStream.pipe(peer)\n})\n\n// If you want to connect to an existing tree\ntreeClient.connect(treeID)\ntreeClient.on('discover', function (treeID) {\n  // Tree exists, but we may not be connected to an upstream peer yet\n})\ntreeClient.on('upstreamPeer', (peer) =\u003e {\n  peer.pipe(writableStream) // this is our stream data\n})\ntreeClient.on('downstreamPeer', (peer) =\u003e {\n  readableStream.pipe(peer) // either replicate the upstream content, or pipe else\n})\n```\n\n## Server API\n\n### `var treeServer = new PeerTreeServer(io, [opts])`\n\nCreate a new PeerTreeServer. One server can handle multiple trees simultaneously.\n\nRequired `io` is an existing **socket.io** instance.\n\nDefault `opts` option object is:\n```javascript\n{\n   k: 4,\n   k1: 10\n}\n``` \n\n## Client API\n\n### `var treeClient = new PeerTreeClient(io, [opts])`\n\nCreate a new PeerTreeClient. Each client can only connect to one tree.\n\nRequired `io` is an existing **socket.io-client** instance.\n\n`opts` will be passed to the `simple-peer` constructor.\n\n`opts.timeout` is the time in milliseconds to wait for a peer to connect. Default is `10000`.\n\n### `treeClient.create()`\n\nCreate a new tree and become the source.\n\n### `treeClient.reconnectUpstream()`\n\nDisconnect from the current upstream peer and request a new one from the server.\n\n### `treeClient.on('discover', (treeID) =\u003e {})`\n\nFires when a tree is joined.\n\n`treeID` is the ID of the tree. Share this with other peer so they can join.\n\n### `treeClient.on('upstreamPeer', (peer) =\u003e {})`\n\nFires when an upstream peer is connected to. This event never fires if this client is the source.\n\n`peer` is a signalled `simple-peer` instance.\n\n### `treeClient.on('downstreamPeer', (peer) =\u003e {})`\n\nFires when a downstream peer is connected to.\n\n`peer` is a signalled `simple-peer` instance.\n\n### `treeClient.on('disconnect', function () {})`\n\nFires when the peer disconnects from it's upstream peer. It will automatically attempt to reconnect. See Notes for methods to handle churn.\n\n### Notes\n\n#### Security Considerations\n\n**peer-tree** does not attempt to validate data from upstream peers (they can provide any data stream). Data should be cryptographically signed by the source and sensitive data should be encrypted or sent through secure channels. Peers that don't forward data should be removed from the tree by your application. A signed append-only log is recommended!\n\nIt is very difficult to verify the integrity of MediaStream objects, so their use is discouraged in applications that could potentially have malicious peers.\n\n#### Churn/Peer Drop\n\nPeers won't stay connected forever, and when they do drop, their immediate downstream peers will try to reconnect. peer-tree will automatically reconnect these peers, but it is possible that some data is missed during this reconnection process. Your replication process should consider that new downstream peers only have partial data.\n\nOne way to mitigate this is to create two or more trees with a lower k-value and send the same data along them. This will reduce the chance that a peer is completely disconnected, although it adds some redundant data transfer.\n\nIt is always possible that peers in the first few levels of the tree will disconnect, causing a large number of peers to lose connection. To prevent this, set k1 to the highest value your source peer can handle and use multiple trees.\n\n#### Latency\n\nAlthough trees suffer from churn problems, they are the ideal architecture for low latency solutions. The number of connections between the source and a newly connecting peer is guaranteed to less than logK(n), where n is the total number of peers and K is the max number of downstream connections. **peer-tree** uses breadth-first order to place peers, but does not attempt to rebalance the tree.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ft-mullen%2Fpeer-tree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ft-mullen%2Fpeer-tree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ft-mullen%2Fpeer-tree/lists"}