28
TVE 15 036 Examensarbete 15 hp Juni 2015 Peer-to-peer distribution of web content using WebRTC within a web browser Kerstin Ersson Siri Persson

Peer-to-peer distribution of web content using WebRTC ...uu.diva-portal.org/smash/get/diva2:819420/FULLTEXT01.pdf · Peer-to-peer distribution of web content using WebRTC within a

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Peer-to-peer distribution of web content using WebRTC ...uu.diva-portal.org/smash/get/diva2:819420/FULLTEXT01.pdf · Peer-to-peer distribution of web content using WebRTC within a

TVE 15 036

Examensarbete 15 hpJuni 2015

Peer-to-peer distribution of web content using WebRTC within a web browser

Kerstin ErssonSiri Persson

Page 2: Peer-to-peer distribution of web content using WebRTC ...uu.diva-portal.org/smash/get/diva2:819420/FULLTEXT01.pdf · Peer-to-peer distribution of web content using WebRTC within a

Teknisk- naturvetenskaplig fakultet UTH-enheten Besöksadress: Ångströmlaboratoriet Lägerhyddsvägen 1 Hus 4, Plan 0 Postadress: Box 536 751 21 Uppsala Telefon: 018 – 471 30 03 Telefax: 018 – 471 30 00 Hemsida: http://www.teknat.uu.se/student

Abstract

Peer-to-peer distribution of web content usingWebRTC within a web browser

Kerstin Ersson, Siri Persson

The aim of this project was to investigate if it ispossible to host websites using the BitTorrentprotocol, a protocol for distribution of data onthe web. This was done using several Node.jsmodules, small clusters of code written inJavaScript, such as Browserify and a modifiedversion of WebTorrent. In these modules,technologies like websockets and WebRTC areimplemented. The project resulted in a workingWebTorrent module, implemented on thewebsite www.peerweb.io. However, the modulestill needs optimization concerning the time ittakes to set up a WebRTC peer connection.With these modifications, we believe thathosting websites via peer-to-peer network willbe the future of the web.

ISSN: 1401-5757, TVE 15 036Examinator: Martin SjödinÄmnesgranskare: Rikard EmanuelssonHandledare: Magnus Lundstedt

Page 3: Peer-to-peer distribution of web content using WebRTC ...uu.diva-portal.org/smash/get/diva2:819420/FULLTEXT01.pdf · Peer-to-peer distribution of web content using WebRTC within a

Populärvetenskaplig sammanfattning

En fråga av stor vikt i dagens informationstekniska samhälle är hur dataeffektivt ska distribueras på nätet. Våra tidspressade scheman kräver att visnabbt kan få tillgång till alla former av data. Den traditionella lösningen, enserver som tillhandahåller information klienter som måste fråga servern innande kan få tillgång till och ladda ner data, är både ineffektiv och tidskrävande.Som tur är finns nya lösningar till detta problem,baserade på ny tekniksom websockets och WebRTC, två verktyg för realtidskommunikation, vilketmöjliggör effektivare metoder att sprida data. BitTorrent är ett protokoll förså kallade peer-to-peer-nätverk, som används bland annat av fildelningstjänster.Trots att BitTorrent mest har använts till att olagligt sprida filmer, musik ochspel på nätet har det också öppnat lagliga möjligheter för företag och enskildaanvändare, exempelvis kan dessa tekniker bidra till att minska bandbreddskostnader.

I detta projekt undersöktes om det är möjligt att implementera ett peer-to-peernätverk som använder sig av BitTorrent-protokollet, websockets ochWebRTCför att dela data som till exempel hemsidor. Målet var att skapa en fungerande,modifierad version av den befintliga modulen WebTorrent, skriven i Node.jsmed programspråket JavaScript, för att kunna överföra dessa filer. Måletuppnåddes och det enda problemet som kvarstod var att processen iblandtog längre tid än önskat. Vid överföring av filer är detta acceptabelt, menvid överföring av hemsidor krävs en högre överföringshastighet. Detta skulledock kunna åtgärdas i en framtida optimeringsprocess.

3

Page 4: Peer-to-peer distribution of web content using WebRTC ...uu.diva-portal.org/smash/get/diva2:819420/FULLTEXT01.pdf · Peer-to-peer distribution of web content using WebRTC within a

Contents

1 Introduction 7

1.1 Precisit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

1.2 Project description . . . . . . . . . . . . . . . . . . . . . . . . 7

2 Theory 8

2.1 Programming languages . . . . . . . . . . . . . . . . . . . . . 8

2.1.1 Python . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

2.1.2 HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

2.1.3 CSS . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

2.1.4 JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

2.1.5 JavaScript and Node.js . . . . . . . . . . . . . . . . . . 9

2.2 Developing environment and services . . . . . . . . . . . . . . 9

2.2.1 GitHub . . . . . . . . . . . . . . . . . . . . . . . . . . 10

2.2.2 Terminal . . . . . . . . . . . . . . . . . . . . . . . . . . 10

2.2.3 Sublime Text . . . . . . . . . . . . . . . . . . . . . . . 10

2.3 Server-client model and peer-to-peer networks . . . . . . . . . 10

2.4 Websockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

2.5 WebRTC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

2.5.1 Signaling . . . . . . . . . . . . . . . . . . . . . . . . . . 12

2.6 The BitTorrent protocol . . . . . . . . . . . . . . . . . . . . . 13

2.6.1 Uploading and downloading files . . . . . . . . . . . . . 14

2.7 WebTorrent . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

2.8 Similar projects . . . . . . . . . . . . . . . . . . . . . . . . . . 15

3 Method 16

4

Page 5: Peer-to-peer distribution of web content using WebRTC ...uu.diva-portal.org/smash/get/diva2:819420/FULLTEXT01.pdf · Peer-to-peer distribution of web content using WebRTC within a

3.1 Introduction projects . . . . . . . . . . . . . . . . . . . . . . . 16

3.1.1 Chat application using websockets . . . . . . . . . . . . 17

3.1.2 Video chat using WebRTC . . . . . . . . . . . . . . . . 17

3.1.3 Chat application using a WebRTC data channel . . . . 17

3.2 Analyzing and developing the WebTorrent module . . . . . . . 17

3.2.1 Modifying the modules . . . . . . . . . . . . . . . . . . 17

3.2.2 Testing during development . . . . . . . . . . . . . . . 19

4 Result 19

4.1 Subprojects . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

4.2 WebTorrent . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

4.3 www.peerweb.io . . . . . . . . . . . . . . . . . . . . . . . . . . 21

5 Discussion 22

5.1 Methods and the development phase . . . . . . . . . . . . . . 22

5.1.1 The subprojects . . . . . . . . . . . . . . . . . . . . . . 22

5.1.2 Developing the WebTorrent module . . . . . . . . . . . 23

5.2 Benefits and disadvantages when hosting websites withWebTorrent 24

6 Conclusions 25

5

Page 6: Peer-to-peer distribution of web content using WebRTC ...uu.diva-portal.org/smash/get/diva2:819420/FULLTEXT01.pdf · Peer-to-peer distribution of web content using WebRTC within a

AbbreviationsAPI Application Programming InterfaceCDN Content Delivery NetworkCSS Cascading Style SheetsDNS Domain Name SystemHTML HyperText Markup LanguageHTTP HyperText Transfer ProtocolICE Interactive Connectivity EstablishmentIM Instant MessagingIP Internet ProtocolJSON JavaScript Object NotationREST Representational State TransferRTC Real Time CommunicationSDP Session Description ProtocolSHA Secure Hash AlgorithmSTUN Session Traversal Utilities for NATTCP Transmission Control ProtocolUDP User Datagram ProtocolURI Uniform Resource IdentificationURL Uniform Resource Locator

6

Page 7: Peer-to-peer distribution of web content using WebRTC ...uu.diva-portal.org/smash/get/diva2:819420/FULLTEXT01.pdf · Peer-to-peer distribution of web content using WebRTC within a

1 Introduction

Recent technologies within modern web browsers enable new solutions toold problems, such as distribution of content (HTML code, CSS sheets,JavaScript, movies, pictures etcetera). The traditional client-server modelis being replaced by new solutions with connections directly between peers,such as websockets (Theory 2.4) and WebRTC (Theory 2.5) peer connections.These new solutions have opened a world of new possibilities for distributionof content in browsers, making it faster and easier than ever to exchangeinformation on the web.

The first service for file sharing through peer-to-peer networks is consideredto be Napster, which was released in 1999 [1]. Since then, several otherclients have been released, and in the early 2000’s the BitTorrent protocolwas presented. The BitTorrent protocol has revolutionized the way peopleshare files on the internet. Even though its main use has been unauthorizeddistribution of movies, games, music, and other copyright data, it is also usedlegally by companies and individuals to cut bandwidth costs.

1.1 Precisit

This project was done at Precisit, a startup company in IT and managementconsulting, at their office in Uppsala. Precisit’s vision is to work with cuttingedge technology that can simplify our everyday life, and possibly change theworld.

1.2 Project description

This project will attempt to implement a distribution network for web contentusing a web implementation of the BitTorrent protocol, WebTorrent. Wewill, however, make a few changes to the WebTorrent source code, in orderto receive desired functionality.

Currently, the host company has a solution for real time signaling between alarge number of peers, called jsFlow.[2] This would be extended with supportfor torrent trackers written in JavaScript to enable distributing content overthe WebRTC data channels used by WebTorrent.

The aim of this project is to investigate the feasibility in using these technologies

7

Page 8: Peer-to-peer distribution of web content using WebRTC ...uu.diva-portal.org/smash/get/diva2:819420/FULLTEXT01.pdf · Peer-to-peer distribution of web content using WebRTC within a

to share data such as web pages, in terms of, inter alia, application-handinessand latency.

2 Theory

2.1 Programming languages

In this project several different programming languages were used. In thissection, these programming languages are listed and shortly explained, sincea background knowledge about these different programming languages isneeded to understand concepts later on in the project.

2.1.1 Python

Python is a programming language used for object oriented and functionalprogramming. It has extensive libraries available for implementing efficientREST-services such as Tornado, which is a library by Facebook for solvingthe C10k problem (>10k connecting per server) and as such is highly scalableand efficient.[3]

2.1.2 HTML

HTML is the standard markup language for creating web pages. The languagemostly uses tag pairs to create elements, one opening tag and one closing tagenclosed by angle brackets. Web browsers use the HTML code to interpretthe content of the web page. The HTML objects are the building blocksof the web pages and allow for the embedding of, for example, videos andimages.[4]

2.1.3 CSS

CSS is a style sheet language used to change the layout of interfaces writtenin a markup language. Together with HTML and JavaScript, CSS forms thecornerstone technology of building engaging web pages and mobile applications.For each HTML object used, a number of formatting instructions are provided.[5]

8

Page 9: Peer-to-peer distribution of web content using WebRTC ...uu.diva-portal.org/smash/get/diva2:819420/FULLTEXT01.pdf · Peer-to-peer distribution of web content using WebRTC within a

2.1.4 JSON

JSON is an open standard format language originally derived from JavaScriptto enable transmission of data in attribute-value pairs. Most programminglanguages has code for handling JSON objects.[6]

2.1.5 JavaScript and Node.js

JavaScript is a dynamic script language which is mostly used on the clientside in web applications. Many JavaScript developers use the module patternwhen coding, which is a way of dividing bigger programs into smaller clustersof code. An example of this is Node.js, which allows JavaScript to be usedon the server side as well as allows connections to databases, sending emailsand so on. The idea behind Node.js modules is that they each should do onething, and do that well. Thus, bigger and more complex programs can thenbe built using these modules as building blocks.

npm is a package manager for Node.js, where Node developers can uploadtheir modules for other people to use in their programs. npm has over70 000 modules that handles anything from video streaming to advancedmathematics and encoding. Modules are imported to Node by using thecommand require(’module’).[7]

In the browser, however, the require method is not defined. But softwarewritten for Node.js can be compiled to run in the browser environment byusing Browserify, a tool that compiles Node.js style modules for use in thebrowser.

JavaScript is also used in Precisit’s real-time messaging service jsFlow, whichuses websockets to enable users to send JSON-objects.[8]

2.2 Developing environment and services

To be able to develop our own code in an efficient and practical way, weused Github, the Terminal and Sublime text. These three services will beexplained further in this section.

9

Page 10: Peer-to-peer distribution of web content using WebRTC ...uu.diva-portal.org/smash/get/diva2:819420/FULLTEXT01.pdf · Peer-to-peer distribution of web content using WebRTC within a

2.2.1 GitHub

GitHub is a service for hosting source code files as Git repositories on theweb. GitHub is often used for open source software projects and it is thelargest code host on the web. Users can fork a repository to modify the codeand then apply for the original poster to approve these changes. GitHub isalso useful when several collaborators work together on a repository.[9]

2.2.2 Terminal

The terminal is an interface in which you can type commands directly tothe computer. It also gives access to the computers operating system. Theterminal used in this project is OS X operating system by Apple.[10]

2.2.3 Sublime Text

In the development of the source code, we used the text editor Sublime Text.It supports many programming and markup languages. One of the functionsthat makes this text editor user-friendly is the multiple selection, which allowsthe user to edit several lines in the code at the same time. Another helpfulfeature is the command palette, enabling the user to search easily withouthaving to navigate through several menus. Sublime text supports manyprogramming languages such as JavaScript, Python, JSON, C++, MATLAB,HTML and CSS.[11]

2.3 Server-client model and peer-to-peer networks

The server-client model is a structure for the distribution of data, in whichwe divide the task between the resource, the server, and the requester of theservice, the client. The server’s task is to provide the data, for example aweb page or a file, which then can be downloaded by the client.[12]

In a peer-to-peer network on the other hand, the nodes (clients) can act asboth server and client at the same time, see Figure 1. The data is partitionedin parts and one client can download different parts from different peers. Atthe same time, it can also act as a server and share the pieces of the file thatthe user already has downloaded. Each peer takes a part of the workload,which results in a lower latency than for the server-client model.[13]

10

Page 11: Peer-to-peer distribution of web content using WebRTC ...uu.diva-portal.org/smash/get/diva2:819420/FULLTEXT01.pdf · Peer-to-peer distribution of web content using WebRTC within a

Figure 1: Peer-to-peer system

2.4 Websockets

Websockets enables users to send data in two directions over a TCP socket.This makes real time-communication between a server and a client in aweb browser possible, which is ideal for different kinds of live content. Awebsocket is event-driven, which means that the server can send messages tothe client, without being polled first. Data can be sent in both directions aslong as the connection is open.[14][15]

2.5 WebRTC

WebRTC is an API for browser-based communication applications that enablesall kind of real-time communication, such as audio/video streaming anddata sharing, between browser clients (peers) without the need for plugins.The two most outstanding advantages with WebRTC is the ease of useand the high security level it provides for the end user. For enterprises,WebRTC can also save money because of the lower bandwith cost, andprovide uninterrupted and enriched communication.[16]

Many web services, such as Skype and Facebook, already use RTC today, butthey need native applications, downloads and plugins to use it. The problemwith plugins is that the downloading, updating and installing processes canbe complex and difficult to troubleshoot. They may also require licenses for

11

Page 12: Peer-to-peer distribution of web content using WebRTC ...uu.diva-portal.org/smash/get/diva2:819420/FULLTEXT01.pdf · Peer-to-peer distribution of web content using WebRTC within a

different kinds of advanced and expansive technology.[17] The idea behindWebRTC is to make real-time communication more user friendly, and tosimplify communication between different smart devices and computers.

2.5.1 Signaling

In order to use WebRTC, a peer connection must be created using signaling,illustrated in Figure 2. There is no set frame for the implementation of thesignaling; it is up to the developer to implement a suitable solution for everyspecial case. One way to do this is to use websockets. The goal with thesignaling is to set up a media stream or a data channel with high performanceand low latency.[18]

To start the signaling process, the local client, who is referred to as Alice,creates a peer connection object and an SDP offer is created and set as thelocal description. The SDP offer is a description of the data communicationsession to sync both peers’ understanding of what the data exchange will looklike, and also how it will be encrypted. [19] The SDP offer is then sent tothe remote peer, called Bob, who will set the offer as the remote descriptionand create an SDP answer to be sent back.

12

Page 13: Peer-to-peer distribution of web content using WebRTC ...uu.diva-portal.org/smash/get/diva2:819420/FULLTEXT01.pdf · Peer-to-peer distribution of web content using WebRTC within a

Figure 2: WebRTC signaling

As soon as the SDP offer has been created, the peer also starts to create ICEcandidates. The ICE candidates contains information on how the peer canbe contacted via a public IP address. This is done by using a STUN server.The STUN server is a network protocol and a set of methods that allows anend host to discover its public IP-address.[20] Alice sends her ICE candidatesto Bob, and Bob sends his back to Alice. Either they store the candidatesand send them all at once, or they use trickle, and send the candidate assoon as it has been generated.[21] Bob and Alice try to contact each otherusing the IP-addresses they receive, until they establish a connection and thesignaling phase is over.

2.6 The BitTorrent protocol

The BitTorrent protocol is one of the most used protocols for file distributionon the internet. In order to download, or upload, files the user has to havea BitTorrent client, like for instance µTorrent. A torrent tracker is used totell the user which files are available for download, and is also used in the

13

Page 14: Peer-to-peer distribution of web content using WebRTC ...uu.diva-portal.org/smash/get/diva2:819420/FULLTEXT01.pdf · Peer-to-peer distribution of web content using WebRTC within a

download itself.[22] The most popular torrent tracker used to be The PirateBay, until the website got into legal trouble in 2009. The site now only worksas an index, offering only magnet links.[23] Magnet links are a relatively newURI which was developed mainly for peer-to-peer networks. They do notcontain any pointers to a specific file, but the hash ID of the file. The hashcode is then used to locate peers that has pieces of the file. They may alsohold additional information, like the URL of a tracker or the file name.

2.6.1 Uploading and downloading files

When a user wants to share a file, they create a torrent-file, which is apointer file with a description of how your computer can find the file tobe downloaded. The torrent-file can be shared through emails or the webfor instance. The uploading user then makes the file available through aBitTorrent node and thereby acts as a seeder. A seeder is a user whichhas the complete file. At least one seeder is necessary for the BitTorrentdownload to operate. When distributing a file, it is considered a numberof equally sized pieces. For each piece a unique hash-ID is created by usingSHA-1, a cryptographic hash function, which is then added to the torrent-file.The hash-ID:s are used by downloaders to check the validity of the pieces.

When other computers want to download the file, they open the torrent-filein their BitTorrent client, which will then connect to the tracker. A torrenttracker is a server that assists in the connection between peers. From thetracker, the users receive information on which users who are currentlydownloading or uploading pieces of the file.[24] The user can now connectto these peers and request pieces of the file and start downloading, therebyacting as leeches (peers who download files but don’t seed) or peers. Whena peer has downloaded a piece of the file, it becomes a source for otherpeers for that specific piece, thus relieving the original seeder of some ofits workload.[22] Together, the peers uploading or downloading a torrent iscalled a swarm.

2.7 WebTorrent

WebTorrent is a node module that does not use any plugins. The ideabehind WebTorrent was to build a BitTorrent client that works in the browserwithout being downloaded, so that sharing data on the internet would be aseasy as watching a video on Youtube.[25] The module is written in JavaScript

14

Page 15: Peer-to-peer distribution of web content using WebRTC ...uu.diva-portal.org/smash/get/diva2:819420/FULLTEXT01.pdf · Peer-to-peer distribution of web content using WebRTC within a

and uses WebRTC in the browser to create a datachannel for peer-to-peertransport. The files are exposed as streams and can be fetched either by usingthe rarest-first or the sequential strategy, depending on the users preferences.WebTorrent is supported by Chrome, Opera and Firefox.Since WebTorrent isan open source project, anyone can find the source code on GitHub, fork therepositories to make edits or contributions, and then submit these changesto the owner with a pull request.[26]

WebTorrent is directly dependent on 43 modules, of which the most importantare listed in Table 1. Each one of these modules in turn require several otherones, creating a complex network where each module is a building block.

Table 1: The main modules in WebTorrent

Module Descriptionwebtorrent torrent client (the main module)bittorrent-dht distributed hash table clientbittorrent-peerid identify client name/versionbitorrent-protocol bittorrent protocol streambittorrent-swarm bittorrent connection managerbittorrent-tracker bittorrent tracker server/clientcreate-torrent create .torrent filesip-set efficient mutable ip setload ip-set load ip sets from local/networkmagnet-uri parse magnet urisparse-torrent parse torrent identifierstorrent-discovery find peers via dht and trackerut_metadata metadata for magnet urisut_pex peer discovery

2.8 Similar projects

A similar project in progress is BitTorrent Inc’s Project Maelstrom, anattempt to launch the first torrent-based browser, seen in Figure 3. Theproject was announced in December 2014 and the Beta version for Windowswas released in April 2015.[27][28]

15

Page 16: Peer-to-peer distribution of web content using WebRTC ...uu.diva-portal.org/smash/get/diva2:819420/FULLTEXT01.pdf · Peer-to-peer distribution of web content using WebRTC within a

Figure 3: The torrent-based browser Project Maelstrom

Another project in the field is Flashback, which is a system created atUniversity of California for handling unexpected increases in traffic on awebsite. The purpose of the project was to find a way to keep citizens of LosAngeles informed in case of an emergency. The system was very similar toProject Maelstrom in terms of functionality.[29]

PeerCDN is a service created by the developers of WebTorrent that servessome website content, like images and videos, via peer-to-peer connectionsto help cut bandwidth costs for the website provider. The site was acquiredby Yahoo in December 2013.[30]

3 Method

3.1 Introduction projects

To learn Python and JavaScript, and to get familiar with the use of websockets,jsFlow and WebRTC, we started with a three subprojects. These will bedescribed shortly in the following section.

16

Page 17: Peer-to-peer distribution of web content using WebRTC ...uu.diva-portal.org/smash/get/diva2:819420/FULLTEXT01.pdf · Peer-to-peer distribution of web content using WebRTC within a

3.1.1 Chat application using websockets

The first subproject was a simple chat application, to get an understandingfor the server-client model and to learn how to set up a websocket connectionand to use it to send and receive data. The chat uses RabbitMQ, whichis an intermediary for messaging in applications to send the messages asJSON-objects. The code for the client side was written in JavaScript, usingNode.js, and the server side was written in Python.

3.1.2 Video chat using WebRTC

The next step was to create a video chat. For this implementation, writtenin JavaScript, the data, video and audio was sent through a media stream,using a WebRTC peer connection. The signaling was done in jsFlow and thescript was incorporated in an HTML document to stream the video in thebrowser.

3.1.3 Chat application using a WebRTC data channel

Finally, a chat application using a WebRTC data channel was created, whichwas also written in JavaScript in Node.js using jsFlow for the signaling. Toimplement the chat in the browser, an HTML document was created togetherwith a few lines of CSS code to style it.

3.2 Analyzing and developing the WebTorrent module

Since most of the changes to be made to the WebTorrent module were veryspecific to the signaling, the code needed to be analyzed to see where this washandled. All other aspects of the code could be left unedited. The modulesthat would need editing were found to be simple-peer and bittorrent-tracker.

Since the tracker part of the WebTorrent module also had to be rewritten,small changes in the module create-torrent had to be done as well.

3.2.1 Modifying the modules

Figure 4 shows howWebTorrent implements the simple-peer, bittorrent-trackerand create-torrent modules. InWebTorrent and torrent-discovery, the changes

17

Page 18: Peer-to-peer distribution of web content using WebRTC ...uu.diva-portal.org/smash/get/diva2:819420/FULLTEXT01.pdf · Peer-to-peer distribution of web content using WebRTC within a

was limited to their dependency specifications, so that they used the rewrittenversions instead. To do the editing, the Git repositories of the modules to bemodified were forked.

Figure 4: Modules in WebTorrent

The simple-peer module handles the setup of the peer connection and thecreation of the data channel. However, none of the messaging of SDP offers orICE candidates is dealt with here. What was needed to be done in simple-peerwas to add a global variable to know whether to buffer or to handle ICEcandidates directly.

In the bittorrent-tracker module, several files had to be modified. Firstly, thefile client.js, which creates a new tracker client and sets the protocol forthe communication between the client and the tracker. In this file, a trackerprotocol for jsFlow was added to the already existing protocols for HTTP,UDP and websockets.

Thereafter, the file jsflow-tracker.js had to be written, in which theactual communication with the tracker is handled using jsFlow. The functionalityof this code is basically the same as for the other protocols, with one importantdifference. In the original WebTorrent module, the SDP offer and the ICE-candidateswere stored by the tracker, and sent once all candidates were generated. Inthe edited version, the method trickle ICE is used, which means that thelocal peer sends the ICE-candidates directly to the remote peer, withoutpassing the tracker, as soon as it has been created. This means that timecan be saved in the signaling phase. Another difference is that the modifiedWebTorrent uses jsFlow for the messaging, both with the tracker server and

18

Page 19: Peer-to-peer distribution of web content using WebRTC ...uu.diva-portal.org/smash/get/diva2:819420/FULLTEXT01.pdf · Peer-to-peer distribution of web content using WebRTC within a

with other peers for the WebRTC signaling.

Another difference is that the announce interval time, which is defined inthe file server.js, was shortened in the new version. The announce intervalis the amount of time passed between each time a peer sends a request tothe tracker server, which will then return a list with the peers working on acertain file.

The addresses for the torrent trackers for each protocol is listed in thecreate-torrent module. The only edit made here was the addition of thejsflow-tracker address to this list.

3.2.2 Testing during development

During the editing phase, two simple test files, upload.js and download.js,were used to test the edited code. These files use a drag-drop module to loadfiles into the browser with the upload.js file. When download.js is openedin another browser, WebTorrent is used to transfer the file and a downloadlink is shown on the download page. Before the files could be used in thebrowser, the dependency modules had to be updated to their last version onGitHub using the command npm update in the Terminal. Then, Browserifycould be used to compile the files to run in the browser.

4 Result

4.1 Subprojects

The chat application developed using websockets in the beginning of thisproject worked as expected. When the server.py file is running in theTerminal, users can join the chat by running the client.js file and settinga username. The users can then join different chat rooms and send messagesto all clients in these rooms. Figure 5 shows what running client.js in theTerminal can look like.

19

Page 20: Peer-to-peer distribution of web content using WebRTC ...uu.diva-portal.org/smash/get/diva2:819420/FULLTEXT01.pdf · Peer-to-peer distribution of web content using WebRTC within a

Figure 5: The chat’s client.js file running in the Terminal

The video chat that was created using WebRTC peer connections also fulfilledthe requirements. The chat enabled users to open a media stream in the webbrowser and communicate with each other through both video and audio.

Finally, the chat application which used a WebRTC data channel to sendmessages, in the form of JSON objects, was also developed successfully.Figure 6 shows the chat running in the browser.

Figure 6: The data channel chat running in the browser

20

Page 21: Peer-to-peer distribution of web content using WebRTC ...uu.diva-portal.org/smash/get/diva2:819420/FULLTEXT01.pdf · Peer-to-peer distribution of web content using WebRTC within a

4.2 WebTorrent

To make the hosting of web pages via WebTorrent an attractive alternative,the modified WebTorrent module needed to work fast with high reliability.The edited module could be used successfully with upload.js and download.js, and a file could be transferred between the two peers. However, theprocess sometimes took several announce intervals. When sharing files, thisis not a very big problem, but it is not good when trying to transfer a HTMLfile for a website.

4.3 www.peerweb.io

To demonstrate the new version of WebTorrent developed in this project,Magnus Lundstedt at Precisit created the website www.peerweb.io. Theweb page’s interface is shown in Figure 7. Users can share regular files, suchas pictures or text files, as well as HTML files containing JavaScript andCSS code, and images. The user drags the file, or file bundle, and drops it inthe web page window, which will generate a file-specific URL with the file’smagnet URI. This URL can then be shared using, for example, email or IMand other peers can then download the file, using the URI. If the data bundlecontains an index.html file, it will be considered a web page.

21

Page 22: Peer-to-peer distribution of web content using WebRTC ...uu.diva-portal.org/smash/get/diva2:819420/FULLTEXT01.pdf · Peer-to-peer distribution of web content using WebRTC within a

Figure 7: The web page for Peerweb.io

5 Discussion

5.1 Methods and the development phase

In this section we will discuss whether the subprojects provided sufficientknowledge to execute the project in a satisfying manner. We will also considerif any part of the project could have been carried out differently.

5.1.1 The subprojects

The subprojects were introduced to provide basic understanding of how thecentral technologies used in this project works, as well as to teach us how toread and modify Node modules.

When we developed the first chat, we learned how to open and send messagesover websockets. We used the programming languages Python, which wasonly used in this subproject, and JavaScript. None of these things wassomething that we used directly later on in the project, but it gave us abetter understanding of the benefits of the technologies implemented. It also

22

Page 23: Peer-to-peer distribution of web content using WebRTC ...uu.diva-portal.org/smash/get/diva2:819420/FULLTEXT01.pdf · Peer-to-peer distribution of web content using WebRTC within a

helped us get familiar with what approaches could be useful when solvingproblems that may arise when programming.

Working with the video chat and the data channel we learned how thesignaling to set up a WebRTC peer connection is done. We also startedto get familiar with different Node modules. This was very useful when weanalyzed WebTorrent, as well as when we started the editing process. Thiswas also where we first came in contact with jsFlow, and learned how to useit.

The subprojects were an essential part of the project for two reasons. Firstly,to learn how to handle all the tools that were later used and get a betterunderstanding on their functions. Secondly, and probably most importantly,it gave us experience in programming and reading JavaScript code and workingwith different Node.js modules. This was fundamental in all work concerningWebTorrent.

Analyzing the work of other programmers is easier the more experiencedyou are. Being well-versed in the programming languages and technologiesused is also fundamental. As we were both new to JavaScript as well as towebsockets and WebRTC, the learning process had to be quite extensive.

5.1.2 Developing the WebTorrent module

The first problem we encountered during the development was the complexityand extent of the WebTorrent module’s source code. To make our ownedited version work correctly we needed to peel back all layers of modulesand identify which changes that was necessary. Missing something couldcompromise the functionality of our module.

Since we have a working version of the edited module we are certain thatwe have made the essential modifications. However, the reliability and speedhas not yet reached desired levels, which means that we have missed somechanges that could improve the user experience. There is also a possibilitythat some functions are inaccurately placed, which could have an impact onthe functionality as well.

Another issue we had to deal with was the buffering of ICE candidates. Atfirst, we wrongfully placed the buffering in the simple-peer module so thecandidates were buffered at the peer that generated them, which was not theidea. This was corrected by moving the buffering to the file jsflow-tracker,where they were handled by the remote peer. At first, we also handled

23

Page 24: Peer-to-peer distribution of web content using WebRTC ...uu.diva-portal.org/smash/get/diva2:819420/FULLTEXT01.pdf · Peer-to-peer distribution of web content using WebRTC within a

the buffered candidates incorrectly when they were processed, which did notaffect the functionality but the time to setup the peer connection. This provesthe importance of locating also the non-essential, but necessary, modificationsneeded for the desired functionality.

We also noted that the peers had more trouble connecting when they useddifferent networks. This is because the NAT makes it necessary to use theSTUN server to identify the public IP address of the peers, in order toestablish a connection. However, we did not have the time to address thisproblem.

Internet users today have high demands on speed, and in order to competewith current methods for hosting websites our WebTorrent module will haveto be optimized.

5.2 Benefits and disadvantages when hosting websiteswith WebTorrent

There are two prominent benefits with hosting websites via WebTorrent.The first one is that these sites can handle a higher work load than regularweb hosting services. This is especially beneficial when the reliability of thesite is essential, for example when streaming big live events, or to spreadinformation in case of an emergency. Overwhelming servers with traffic isa common technique used by hackers to make a website unavailable. Thiscould be prevented if the site was not dependent on a server.

The second important benefit is that it would be harder for governments andauthorities to control what content should be accessible on the web. Even ifthey tried to censor or remove certain content, the data could be reassembledusing data chunks from different peers.

However, there is also a downside to this. There are sites with illegal orunethical content, that authorities even today struggle to combat. This battlewould become even harder, since the content of a website would be accessibleas long as people keep visiting it. Also, if more traffic means more peers toseed the site’s content, less traffic would mean that a site could drop off thegrid. It would also be harder for new websites to get established.

Another problem with hosting websites via WebTorrent, is that you haveto have some of the website’s content on your computer to seed. Biggerwebsites, with many images and videos, will thus require more of the seedersstorage.

24

Page 25: Peer-to-peer distribution of web content using WebRTC ...uu.diva-portal.org/smash/get/diva2:819420/FULLTEXT01.pdf · Peer-to-peer distribution of web content using WebRTC within a

Though there are a few disadvantages with this new technology, there are alsomany benefits. WebTorrent is safe to use, as it only uses a third party duringthe signaling. The server never sees the data that is transferred between twopeers, and thus, the only risk of being hacked is during the signaling phase.

Sites that would benefit especially from this kind of content distribution isso called CDNs. These sites distribute data in several places, and reliabledelivery is not cheap using the technology of today.

6 Conclusions

Our initial question was whether it was feasible to use the idea behindWebTorrent to distribute data, such as websites, in an efficient way, andthe answer is yes. The WebTorrent implementation we made worked, but toget sufficient speed and reliability, the peers had to be connected to the samenetwork. This is something that could be corrected in the future, to makethe experience more pleasant. Other possible future improvements includedecreasing the latency and working on the reliability of the peer connections.

We believe that the sharing of all kinds of data through peer-to-peer connectionsis the future of the web. The ideal is to be able to establish these connectionswithout having to use a server for the signaling, which would eliminate allrisks of being hacked. The way that the BitTorrent protocol revolutionizedfile sharing, the idea behind this project could revolutionize the entire web.

25

Page 26: Peer-to-peer distribution of web content using WebRTC ...uu.diva-portal.org/smash/get/diva2:819420/FULLTEXT01.pdf · Peer-to-peer distribution of web content using WebRTC within a

References

[1] http://en.wikipedia.org/wiki/File_sharing Accessed onMay 29th 2015

[2] http://www.jsflow.com Accessed on May 29th 2015

[3] http://en.wikipedia.org/wiki/Python Accessed on May29th 2015

[4] http://en.wikipedia.org/wiki/HTML Accessed on May 29th2015

[5] http://en.wikipedia.org/wiki/Cascading_Style_SheetsAccessed on May 29th 2015

[6] http://en.wikipedia.org/wiki/JSON Accessed on May 29th2015

[7] https://www.npmjs.com/ Accessed on May 29th 2015

[8] http://en.wikipedia.org/wiki/JavaScript Accessed onMay 29th 2015

[9] http://en.wikipedia.org/wiki/GitHub Accessed on May29th 2015

[10] http://en.wikipedia.org/wiki/Terminal Accessed on May29th 2015

[11] http://www.sublimetext.com Accessed on May 29th 2015

[12] http://en.wikipedia.org/wiki/Client%E2%80%93server_model Accessed on May 29th 2015

[13] http://en.wikipedia.org/wiki/Peer-to-peer Accessed onMay 29th 2015

[14] http://en.wikipedia.org/wiki/WebSocket Accessed on May29th 2015

[15] https://developer.mozilla.org/en/docs/WebSocketsAccessed on May 29th 2015

26

Page 27: Peer-to-peer distribution of web content using WebRTC ...uu.diva-portal.org/smash/get/diva2:819420/FULLTEXT01.pdf · Peer-to-peer distribution of web content using WebRTC within a

[16] http://en.wikipedia.org/wiki/WebRTC Accessed on May29th 2015

[17] http://www.html5rocks.com/en/tutorials/webrtc/basics/Accessed on May 29th 2015

[18] http://danristic.com/html5/javascript/webrtc/2013/08/13/using-the-webrtc-data-channel.html Accessed on May29th 2015

[19] http://en.wikipedia.org/wiki/Session_Description_Protocol Accessed on May 29th 2015

[20] http://en.wikipedia.org/wiki/STUN Accessed on May 29th2015

[21] https://www.pkcsecurity.com/blog Accessed on May 29th2015

[22] http://en.wikipedia.org/wiki/BitTorrent Accessed onMay 29th 2015

[23] ttp://en.wikipedia.org/wiki/The_Pirate_Bay Accessed onMay 29th 2015

[24] http://en.wikipedia.org/wiki/BitTorrent_trackerAccessed on May 29th 2015

[25] https://www.youtube.com/watch?v=PT8s_IVWDgw Accessedon May 29th 2015

[26] https://www.npmjs.com/package/webtorrent Accessed onMay 29th 2015

[27] http://blog.bittorrent.com/2014/12/10/project-maelstrom-the-internet-we-build-next/Accessed on May 29th 2015

[28] http://blog.bittorrent.com/2014/12/10/project-maelstrom-the-internet-we-build-next/Accessed on May 29th 2015

[29] http://www.ics.uci.edu/~mayur/papers/flashback-dcs07.pdf Accessed on May 29th 2015

27

Page 28: Peer-to-peer distribution of web content using WebRTC ...uu.diva-portal.org/smash/get/diva2:819420/FULLTEXT01.pdf · Peer-to-peer distribution of web content using WebRTC within a

[30] http://techcrunch.com/2013/12/17/yahoo-acquires-peercdn/ Accessed on May 29th 2015

28