Skip to content

Instantly share code, notes, and snippets.

@arpit-saxena
Last active May 28, 2019 10:30
Show Gist options
  • Select an option

  • Save arpit-saxena/e957e9c4b108c5bfa61168e20a456de3 to your computer and use it in GitHub Desktop.

Select an option

Save arpit-saxena/e957e9c4b108c5bfa61168e20a456de3 to your computer and use it in GitHub Desktop.
Workflow for the WebTorrent part of FileSend

Workflow

Note

  • Socket emits are shown with the path in bold in the beginning
  • Name is after that
  • Sub points contain the payload

Pre requisites (Assumptions)

  • Two users are connected.
  • Calling them user1 and user2.
  • user1 wants to send a file to user2
  • user1 has selected a file and user2 approved the file request

WebTorrent workflow

  • Torrent clients are created for both user1 and user2 (through new Client, Client is a custom class)
    • The WebRTC config is passed to these clients
  • user1's client starts seeding the file
    • TRACKERS are passed here
    • When torrent is ready (client.on('torrent'..))
      • user1 to server fileReady
        • torrent.magnetURI
      • server to user2 addTorrent
        • torrent.magnetURI
  • user2 on getting addTorrent:
    • client.add (magnetURI) (Also announces to tracker)
    • Also add ontorrent callback to previous function to show progress
  • user2 on completing download:
    • user2 to server downloadComplete
    • server to user1 torrentDone
    • On receiving 'torrentDone', user1 calls torrent.destroy()to stop seeding.
  • Client class is also an EventEmitter and has the following emits:
    • error
      • err: string | Error
    • upload (Emitted whenever new packets are uploaded, for showing upload stats)
      • uploaded: string (formatted as '20 MB' for example)
      • uploadSpeed: string (formatted as '4 MB/s' for example)
    • download (Emitted whenever new packets are downloaded, for showing download stats) (Note: these have same format as above)
      • downloaded: string
      • downloadSpeed: string
      • progress: number (between 0 and 1)
    • downloadComplete
      • url: string (Downloaded file's BlobURL)
    • torrentDestroyed (the torrent that was downloading/seeding was destroyed)
    • clientDestroyed (WebTorrent client was destroyed, possibly due to some error)
  • Misc:
    • torrent.timeRemaining can used to show user2 the time remaining for download to complete
    • torrent.downloaded shows how much was downloaded
    • torrent.uploaded can be used to show user1 how much progress has been made (can't be used if onetomany is required)
  • On download completion, for both user1 and user2:
    • client.remove(torrent) Docs claim it removes all saved data, so will have to check to what extent it deletes stuff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment