Browser-based Peer to Peer file transfer

On your home LAN, it is easy to send files from one device to another, but when you are away from home, things get trickier. Of course, if you are visiting a friend's home, a USB drive can be a useful tool for transferring files, but what if no one is available to move them? It is not uncommon to have a bunch of media files to share after a trip with family or friends. You can share these on social media (this is what they want you to do). However, there are several reasons you might want to keep them private, so the next question is how to share them without using cloud services. 

Email comes to mind, but there are usually limits on the maximum size allowed for messages, and you might overload some recipients' mailboxes to the point where they fail to receive some of the content. And if you are a bit worried about using cloud services, email also involves some Internet servers, too.  Some peer-to-peer file sharing apps exist, but they are typically designed to allow files to be shared among a group of people, where access is generally unrestricted. On the other hand, our use case here is mostly private, with a small number of users. Of course, you could use BitTorrent or a similar technology, creating your own tracker and emailing the torrent files to your relatives. However, this requires some setup, and it only works for you to send files to them, not the other way around.

During the holidays, my project was to create FamilyLAN, a system that would integrate several technologies to enable a community of users, such as family members, to share files with each other. I wanted to do this easily, so no other tool is needed beyond a browser. You can get the full source code here.

Some piping is needed

Transferring files from one computer to another requires a connection. Internet Sockets are a common tool for performing such tasks, but it's tricky if you need to connect from home to home. That is not necessarily a bad thing, as for one, it keeps some bad actors from accessing our home network. But it makes our little project a bit more complicated.

Without getting too technical, our home router usually includes a NAT box, which translates between the internal, private IP addresses and ports and the [public] ones used on the Internet. This process is straightforward when a user at home initiates a connection, such as when you open a web page, but it does not allow incoming connections from the Internet to reach your computer, at least not without some configuration changes. 

We need some tools that will enable incoming data from the Internet to reach our computer, as well as a method for two browsers to communicate and exchange information with each other, so they can agree on a file transfer. What I have used is a couple of pieces of software, namely STUN and WebRTC, to make things work, all tied together with a bit of PHP and JavaScript sorcery. Not visible, but also used, is a SQL database to provide data persistence on the server side. This allows us to create a room for a given family name, where users can join in and start sending files to each other. However, rooms and users are ephemeral, and after a short while of inactivity, they are removed from the database.

With this arrangement, file transfer is synchronous; that is, files are sent from A to B while both users have their browsers open. The files are transferred from one computer to another without involving the web server (except as a meeting point), which means this will not exceed the data transfer quota of the hosting service.

Incidentally, this also works between two of your home computers, or even between two tabs of your browser, though the usefulness of the latter is questionable. 

Performance is ok, but it is not great. Ensure the browser tab remains focused for optimal results. Let me know what you think.


Comments

Popular posts from this blog

VFD control with Arduino using RS485 link

Importing OpenSCAD designs into Onshape

Arduino mood light