Do we still need a server? P2P distribution for Web applications
P2P content distribution allows to reduce server load which is going to be interesting for social and non-profit organizations and projects, also this may decrease network latency since peering could be set up in the way content to be delivered from the nearest peer available. For example after hitting corporative network application will be delivered inside of it using high speed internal channels without creating a load on company`s internet channel.
Pic.1 – Traditional app distribution. Server sends package many times, corporative internet channels are loaded appropriately.
Pic.2 – In case of P2P distribution application hits corporative network once and then is distributed using high speed internal network. This reduces server load and corporative internet channel load.
Or another case – once application got from USA to Europe it is delivered inside of European networks only without creating transatlantic traffic.
Pic.3 – It takes a lot of transatlantic trips to transmit an app when doing it in a traditional way.
Pic.4 – P2P allows to reduce number of transcontinental transfers and reduce server load.
By distributing application via P2P you create a self-establishing and self-evolving CDN which moves data closer to client.
How does viral Javscript work?
- When server receive a request for an app it determines are there any peers available nearby the requester.
- If there are no matching peers client receives full application code from server and small viral script (viral addition) is added to the end of application code. Viral addition executed on client after app transferred completely. It connects to server and notify server about one more peer available. Also viral addition is responsible for serving P2P requests from other clients, basically this is a code which send application to next peer;
- If server finds a correct peer it respond with a minimalistic script (thin client) which contains instruction to connect with peer X`s viral addition, receive application from there and deploy it locally;
- Server helps seeding peer and receiving peer to exchange P2P addresses;
- They establish direct connection and carrier sends application to the recipient;
- Recipient executes code it received which launches application and makes him one more active peer.
Algorithm is simple enough but requires pretty a lot of code to run and some details to be taken into account: How to send static files? How to cache content? How to render app on carrier? How to exchange P2P adresses? Easy answer to all of them can be ViralJS.
ViralJS is a ready to use viral container implementation. To start distributing your app through P2P it is enough to include ViralJS into your project and use it as Express middleware:
var ViralContainer = require('ViralJS'); var viralContainer = new ViralContainer(); app.use(viralContainer.middleware);
Also ViralJS have an API for messaging from carrier to recipient and signage of transmitting content. So for sending object to recipient you may call
, and recipient may read this data via