P2P Video Streaming using Torrent & WebRTC

A proof of concept for serverless peer-to-peer video streaming similar to YouTube.

Ragul.H
4 min readAug 12, 2023
Photo by Cookie the Pom on Unsplash

In this blog, I’ve described how to stream videos without a server. As the founder of Infyrec, I also feel a need to provide self-hosted video courses and recordings to the students enrolled in my course. I actually have plans to develop a YouTube-like video streaming service. However, I currently have to invest money on servers and storage if I want to offer a self-hosted streaming service. In this case, I did research to find a different approach to develop the streaming system and found two distinct technologies that have been available for a decade now: Torrent & WebRTC.

What is torrent?

Torrent is a peer-to-peer file sharing protocol. Users download and upload parts of a file simultaneously from multiple sources, called peers. A torrent file contains metadata and a list of trackers that help connect peers. The more peers sharing a file, the faster the download. Torrent clients use a distributed approach, optimizing speed and reliability.

What is WebRTC?

WebRTC (Web Real-Time Communication) is a technology that enables real-time audio, video, and data sharing directly between web browsers. It uses a combination of JavaScript APIs and peer-to-peer communication to establish connections. This technology is commonly used for video conferencing, online gaming, and other real-time applications.

WebRTC + Torrent = WebTorrent

WebTorrent is a groundbreaking technology that enables efficient peer-to-peer file sharing directly within web browsers. With WebTorrent, users can seamlessly stream and share media, such as videos, without the necessity of a centralized server. This decentralized approach optimizes speed and reliability by distributing the workload among peers, while also allowing content creators (like me 😁) to offer self-hosted streaming services.

Proof of Concept (PoC) Implementation

Let me add a few additional things regarding torrent and WebRTC before moving on to the implementation procedures. Basically, we need to utilize a torrent client software like uTorrent or qBittorrent anytime we want to share or download a file using the torrent protocol, but the drawback is that we won’t be able to watch the videos until the download is finished. The good news is that WebTorrent was able to overcome this issue, therefore we are no longer dependent on torrent clients.

Requirements:

Client Side Code

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="webtorrent.min.js"></script>
</head>
<style>
video{
width: 400px;
height: 200px;
}
</style>
<body>
<h1>P2P Video Streaming PoC (Client)</h1>
<script>
const client = new WebTorrent()

const link = 'YOUR_CUSTOM_GENERATED_MAGNET_LINK'

client.add(link, { announce: ['ws://YOUR_LOCAL_IP:8000'] }, function (torrent) {
const file = torrent.files.find(function (file) {
return file.name.endsWith('.mp4')
})
file.appendTo('body')
})
</script>
</body>
</html>

<!--
Note: Add/update your magnet link and IP address
-->

Server Side Code

Note: I’m simply using my computer as a server in this case. Actually, there is no server involved; I am simply utilising node.js and hybrid webtorrent to seed the video file from my computer.

import WebTorrent from 'webtorrent-hybrid'
const client = new WebTorrent()

client.seed('PATH_TO_YOUR_FILE', { announce: ['ws://YOUR_LOCAL_IP:8000'] }, function(torrent) {
console.log('Sample Video Magnet Link: ' + torrent.magnetURI)
})

Bittorrent Tracker Command

Note: Since there are currently several torrent trackers available online, using a bittorrent tracker is entirely optional. But the reason I’m using it is because I don’t want everyone to access my torrent data.

bittorrent-tracker

Final Outputs:

Client side output:

Server side output:

Bittorrent output:

Explanation:

Let me begin with the server side (backend) first. I installed the appropriate webtorrent packages and added my self-hosted bittorrent tracker server ip in the backend. The magnet link will be printed once I run the code. In addition, I updated the code on the frontend (client side) with a magnet link that I copied from the backend and embedded the webtorrent script. In the end, I just used the live server (a vscode addon) to serve the frontend.

As a result, I was able to successfully stream the video. 🥳🥳🥳

Conclusion

I have described how I built (local) peer-to-peer video streaming in this blog. I’ll describe how to make these things accessible from the public internet with the help of cloud in my next blog.

Thank You

If you have any questions, please send an email to enquire@infyrec.in

--

--

Ragul.H
Ragul.H

Written by Ragul.H

Co-founder of Infyrec | Freelance App Developer

No responses yet