stungun
v0.0.20210828
Published
highly scalable webrtc mesh-like network for data channels
Downloads
2
Readme
What is this?
StunGun is a modification of GUN that uses as much webrtc peer to peer goodness as possible. It's an experiment to see if we can make a network of millions of webrtc connections and use them to transfer data amongst eachother.
How it works
Peer discovery
For each application, we'll use a different tree and save/load it using GunJS, a peer to peer protocol that can be used as a database. When a user wants to join, they specify their desired application. Gun looks for a tree with that application name, and creates one if it does not exist. An optional "desiredUploadSpeed" (in kbs) is provided, otherwise it defaults to 50 (sufficient for real time audio streaming). StunGun checks the user's bandwidth capabilities and divides their available upload speed by the desiredUploadSpeed to calculate the maximum number of children they should have connected to them. This info is saved in the "meta" property of their node object that is soon saved into Gun for other peers to see. StunGun takes the tree fetched through Gun and traverses it using breadth-first-search until it finds a node that has less children than their maximum allowed number of children. The client then connects to that node using PeerJS.
Prophet Nodes
Prophet nodes sit at the highest level of the tree. They are the nodes with the highest bandwidth,
Data transmission
When a user would like to send some data to other peers
Examples
The examples folder contains different apps (only one right now) that demonstrate how StunGun can be used for real-time purposes. There is no real benefit to the design of StunGun over any other webrtc framework if you are only using a handful of clients. To really demonstrate the power of StunGun, you should have dozens of active clients. Sadly, this will likely degrade your performance if you are running all these tabs on one machine, so in practice it might actually be difficult to test by yourself. I will host each example application and link to it here, and doing a genuine test might require some group coordination (or this package getting really popular and people using the demos frequently). Eventually, these apps should serve a real purpose such that they can be used as real applications and benchmarks for StunGun.
Instant Messenger
This is a React app written in Typescript. Pretty self explanatory, it allows many people to send messages to eachother.
Different Methodologies
I can see two possible design systems that would vary in difficulty/simplicity and performance. Both methods involve every peer on the network being ultimately connected through a series of peers, but they differ in performance.
Application Targeted Grouping
Peers using the same application are more closely grouped together. This means that latency would be reduced when peers are talking to eachother over the same app, which is definitely the more likely scenario anyways.
Global tree
Peers' positions in the tree are only determined by what will give them the best performance when communicating with any random peer on average. This is probably easier to implement, but I'm not sure how "real-time" things get when you have millions of peers connected.
Helpful articles
This blog post talks about some of the challenges with building a large-scale webrtc mesh network. https://bloggeek.me/webrtc-p2p-mesh/
Some rambling...
I'm not sure how this will/should work. I'm going to try to just use the GUN API as much as possible but without using any relay peers. My naive solution is this...
- create a layered mesh network of webrtc connections/data channels
- use GUN to save data locally,
- when new data occurs, send it over the mesh network with instructions for GUN
- when the clients receive the data and instructions, they simply save it locally with GUN