Creation of metadata for the binary blocks to send via WebRTC datachannel
4 stars based on
The real creation of metadata for the binary blocks to send via webrtc datachannel of a data channel comes when combining it with other powerful technologies from a browser.
By opening up the power to send data peer-to-peer and combining it with a File API, we could open up all new possibilities in your browser. This means you could add file sharing functionalities that are available creation of metadata for the binary blocks to send via webrtc datachannel any user with an Internet connection. The basics of our application will be real-time, meaning that the two users have to be on the page at the same time to share a file. There will be a finite number of steps that both users will go through to transfer an entire creation of metadata for the binary blocks to send via webrtc datachannel between them:.
The other user will be notified of the file that is being shared, where it will be transferred to their computer over the connection and they will download the file. The main thing we will focus on throughout the chapter is how to work with the data channel in new and exciting ways. The interactivity that the API promotes will stand out in this chapter and can be used in a simple project that can easily be expanded after reading through the book. There is a good chance you have interacted with the File API on a web page and have not even realized it yet!
Before the days of the file input, application developers had to rely on third-party tools to request files of data from the user. This specification was proposed in order to make a standard way to upload files for a server to download, save, and interact with.
This means providing access to not only a way for the user to upload a file, but also ways to read the file in different formats, manipulate the data of the file, and then ultimately do something with this data. This is the ability to get binary file data from the user by asking them to upload a file.
A typical application that works with files, such as Notepad on Windows, will work with file data in pretty much the same way. It asks the user to open a file in which it will read the binary data from the file and display the characters on the screen.
The File API gives us access to the same binary data that any other application would use in the browser. This is the great thing about working with the File API: To start building our application, we will put together another simple web page. This will look similar to the last ones, and should be hosted with a static file server as done in the previous examples.
By the end of the book, you will be a professional single page application builder! One of the main differences is the appearance of the file input, which we will utilize to have the user upload a file to the page. I even picked a different background color this time to spice things up. You can copy the files into a new folder for our file sharing application and add the HTML shown in the preceding section.
The server will return with a success message telling the user they are logged in. Once the peer connection is created, we connect the users through a data channel so that we can send arbitrary data across. Hopefully, this is pretty straightforward and you are able to get this code up and running in no time. It should all be familiar to you and if you have any questions, you can refer to the respective chapters on what you are missing. This is the last time we are going to refer to this code, so get comfortable with it before moving on!
The first thing the user needs to do is select a file from their computer's filesystem. The browser will allow the user to select a file from their computer and then save a reference to that file in the browser for later use. To do this, you need to add an event listener, as shown in the following code:. You might be surprised at how simple the code is to get this far! This is the amazing thing about working within a browser.
Much of the hard work has already been done for you. Now, you might think that the object we get back will be in the form of the entire data inside of our file.
Let's take a look at this metadata:. It will also give a few other important details, such as the type of file we are sending and when it has been modified. The next step is to read the file's data and send it through the data channel. This is no easy task, however, and we will require some special logic to do so.
This will take the entire file and give it to creation of metadata for the binary blocks to send via webrtc datachannel data channel to process as one large message:. Once this happens, the data channel will take the message and try to send all the data through the connection between the clients.
This is where the problems start to arise. If the user is on a bad network connection, the message could fail to send. This means that both users have to have a solid connection for the hours of time that it would take to send the entire file! A network such as BitTorrent will split up large files into hundreds or even thousands of smaller pieces so that it is easy to transport between users.
This is also similar to the concept of shipping containers. We can apply this concept to our file just as easily. Consider a file to be one giant stream of 0s and 1s all in a row. This is known as our binary file data. The easiest way to chunk this file is to read a few lines of binary, send it over, then read the next few lines of binary and send this over. We can then keep doing this all the way until the end of the file. If we lose connection, we can just start over at the last chunk we sent, meaning the entire progress is not lost.
A lot of the work is not done for you and logic will have to be put in to handle your specific use case. It is built as a general solution for most problems, so there is a lot of work to be done to make use of the data channel.
In our previous example, all of our data was already in a string format. This is due to the fact that it was in the form of simple text messages. This is a different story in the case of the File API, however. Files, such as images, audio, and more are not easily converted into human-readable strings, so we will have to work with sending binary data to the other user.
To get our code started, we will define a few simple methods that will encode and decode our data. You can define these anywhere in your script so that we can use them throughout the application that we are going to build:. Now that we can encode data from one user, we need a function to decode data on the other side:.
This is typically used when reading lower-level binary data creation of metadata for the binary blocks to send via webrtc datachannel files. Setting up our initial variables and getting a reference for the buffer of file data. We then make sure we are not past the end of the file since there is nothing to read past the end.
The data is encoded and sent in the Base64 format using the functions we set up earlier. If this is the last chunk, we tell the other user that creation of metadata for the binary blocks to send via webrtc datachannel have completed sending the file. If we still have data to send, we send the next chunk after a certain amount of time to avoid flooding the API. You should be able to connect two users, select a file, and see the chunks being received on the other side.
Go ahead and play with the example to see what happens when you send smaller files as well as larger ones. This means we can put them together in one large data object as we receive them from the other side. To store our pieces of data, we can push them into a simple array data structure in the client. We will do this directly when the data channel reports that a message has been received.
Initially, we will try to use the JSON parser to decode the contents of the message. This is what we will use to inform us if the message is a file data or a command coming from the other user. If the message is not in JSON format, we will assume that this is a chunk of file data. The data will get decoded and inserted into our list of data. This will contain all of the chunks that we get until we save the file later on.
Once we finally get the end event, we need to save the file to the user's system.