Code Monkey home page Code Monkey logo

p2pt's Introduction

P2PT

Simple library to establish P2P connections, communicate messages (even large content!) using WebTorrent WebSocket Trackers as the signalling server.

Used in P2Wiki

Features

  • Easy to use API
  • Send long messages: Data splitted into chunks, sent, received and reassembled all by the library !
  • Use WebSocket Trackers as signalling servers
  • JSON messaging system
  • Send & Respond to messages in a chain using Promise

How Does It Work ?

The amazing WebTorrent library created a new kind of Torrent Trackers called "WebSocket Trackers" also known as "WebTorrent Trackers". Some torrent clients can use these new trackers to share files.

Browser torrent clients (example: BTorrent) only have the capability to communicate to these WebTorrent trackers and other browser peers (known as web peers). Because, JavaScript in browser can't directly make TCP/IP connections and communicate. Read more about how WebTorrent works here.

WebRTC is the method by which browsers can communicate to other browsers peer to peer (P2P). WebTorrent makes use of WebRTC for sharing Torrents on web.

But, to establish P2P connections, a signalling server is needed. Signalling servers can be made in any way. But, you'll have to host it yourself. In WebTorrent, it's the WebSocket trackers that are the signalling servers. What if we use those trackers to establish P2P connections for our apps ?! That is what P2PT does ! :)

How do we find peers for torrent to download ? We use a magnet link. That magnet link has a unique identifier for our torrent called the Info Hash. This ID will be unique for all torrents.

Similarly, to build our apps, we use a identifier. This identifier is converted to a valid Info Hash and sent to our WebTorrent trackers who will give us a list of web peers. These web peers would be the other users also using our app :

var p2pt = new P2PT(trackersAnnounceURLs, 'myApp')

And that is how P2PT works.

Examples

Apps Built With P2PT

  • P2Wiki: Decentralized P2P proxy to access Wikipedia
  • P2Chat: P2P noregister instant chat
  • Vett: P2P Dots-and-Boxes game. Play Here

Code

const P2PT = require('p2pt')

// Find public WebTorrent tracker URLs here : https://github.com/ngosang/trackerslist/blob/master/trackers_all_ws.txt
var trackersAnnounceURLs = [
  "wss://tracker.openwebtorrent.com",
  "wss://tracker.sloppyta.co:443/announce",
  "wss://tracker.novage.com.ua:443/announce",
  "wss://tracker.btorrent.xyz:443/announce",
]

// This 'myApp' is called identifier and should be unique to your app
var p2pt = new P2PT(trackersAnnounceURLs, 'myApp')

// If a new peer, send message
p2pt.on('peerconnect', (peer) => {
  console.log('New Peer !')
  peer.send('Hi').then(([peer, msg]) => {
    console.log(msg)
    return peer.respond('Bye')
  }).then(([peer, msg]) => {
    console.log(msg)
  })
})

// If message received from peer
p2pt.on('msg', (peer, msg) => {
  console.log(`Got message from ${peer.id} : ${msg}`)
  if (msg === 'Hi') {
    peer.respond('Hello !')
  } else if (msg === 'Bye') {
    peer.respond('Bye !')
  }
})

p2pt.start()

Open the page with the above code in two separate browser windows, and open the console. You'll see the messages.

p2pt's People

Contributors

subins2000 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.