Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
WebRTC Blueprints
WebRTC Blueprints

WebRTC Blueprints: Develop your very own media applications and services using WebRTC

eBook
€18.99 €27.99
Paperback
€35.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

WebRTC Blueprints

Chapter 2. Using the WebRTC Data API

This chapter introduces the Data API topic of the WebRTC technology. You will learn what the Data API is and how to use it to develop rich media applications. For practice, we will develop a simple, peer-to-peer file sharing application. You will also get a brief introduction on using the HTML5 File API.

Introducing the Data API


The WebRTC Data API introduces the RTCDataChannel entity, which enables direct data connections between peers (just like for audio and video media channels), as we learned in Chapter 1, Developing a WebRTC Application.

Used in pair with RTCPeerConnection and utilizing the ICE framework, it makes it possible to link up direct data connections through firewalls and NATs. The Data API can be used to build powerful, low-latency applications such as games, remote desktops, desktop sharing, real-time private text chats, peer-to-peer file sharing, and torrents.

Data channels in WebRTC have built-in security. They are usually even faster than WebSocket connections (browser-to-server), because connections proceed between browsers without any point in the middle. Nevertheless, if you turn on the server, data will be transmitted via the server (consuming additional bandwidth on the server side). Also, connection establishment using data channels takes longer than when using...

Introducing HTML5


In this book, we will use the HTML5 API to perform different tasks with JavaScript while developing our demo applications.

HTML5 itself is the latest standard for HTML and you can find its home page at http://www.w3.org/TR/html5/.

Its ancestor HTML 4.01 came in 1999, and the Internet has changed significantly since then. HTML5 was designed to deliver rich content without the need for additional plugins. The current version delivers everything, from animation to graphics, music to movies, and can also be used to build complicated web applications.

HTML5 is also designed to be cross-platform. It is designed to work irrespective of whether you are using a PC, a Tablet, a Smartphone, or a Smart TV.

We will use HTML5 while working on our WebRTC applications; in this chapter, we will start with the HTML5 File API.

Introducing the HTML5 File API


Data channels help us to transmit data (files) between peers. Nevertheless, it doesn't provide any mechanism to read and write files. So, we need some mechanism to be able to read/write files.

For the purpose of filesystem access, we will use the HTML5 File API. It provides a standard way to interact with local files. Using this API, you can create and read files, create thumbnails for image files, save application preferences, and more. You can also verify on the client side; if a customer wants to upload the MIME type of file, you can restrict the uploading by the file size.

When reading files, we will use the FileReader object. It reads files asynchronously and provides the following four ways (methods) to read them:

  • readAsBinaryString: This will read a file into a binary string. Every byte is represented by an integer in the range from 0 to 255.

  • readAsText: This reads the file as a text string. By default, the result string is decoded as UTF-8. You can use...

Known limitations


WebRTC and its Data API are supported by major web browsers. Nevertheless, the WebRTC standard is not yet complete and web browsers can still behave in different ways.

Chrome versions lower than 31 uses the RTP data channel type by default; Firefox uses SCTP by default in all versions.

At the time of writing these lines, Chrome still has some traits in the data channel's implementation. They limit channel bandwidth, which can cause unexpected effects. To fix it, you can use a hack described in this chapter.

With some browsers (not having fully compliant support of the WebRTC API), you can face the unexpected behavior while transmitting big-sized files that cause weird errors.

Nevertheless, all these cases are temporary. You can check the current status of the data channel's implementation on the WebRTC home page at http://www.webrtc.org/home.

Also, you can check the browser's home page for more detailed information on supporting the WebRTC API for a particular browser's version...

Preparing the environment


The whole structure of our application will be the same as in the previous chapter.

We need to create a folder for the new application, for example, filesharing_app. Again, create a subfolder for the HTML/JavaScript part, for example, filesharing_app/www.

We still need a signaling server, and in this chapter we won't introduce any new functionality on the server side. So, you can take the signaling server from the previous chapter. It will work fine for the new application that we're going to develop. You can just copy the signaling server from my_rtc_project/apps/rtcserver and paste it to filesharing_app/apps/.

We will also need the WebRTC API JavaScript adapter that we developed in the previous chapter. The adapter library can be used from the previous chapter with no change, so we need not pay much attention to it in this chapter.

So, take the signaling server application and the myrtcadapter.js library from the application we developed in the previous chapter.

A simple file-sharing service – the browser application


The client code of our new application is very similar to the previous application in some parts. Nevertheless, it will be significantly different in the core side, so we will rework it and discuss it in detail in this chapter.

The WebRTC API wrapper

We developed a wrapper for the WebRTC API in the previous chapter, so now it will be even easier to do the same for our new application. Now we will add some new code relevant to the Data API and for HTML5-specific API functions, which will be useful for file I/O operations, by performing the following steps:

  1. Create the www/myrtclib.js file as we already did in Chapter 1, Developing a WebRTC Application. We need to declare an entity to control the WebRTC peer connection API. Using the myrtcadapter.js library, we will detect the browser type and version, and assign a relevant API function to this entity.

  2. We also need to detect the web browser's version in order to handle it appropriately by...

Developing the main page of the application


As we did in the previous Chapter 1, Developing a WebRTC Application, we need an index.html page where we will implement the UI visible for customers and some additional JavaScript code to implement the client application. This can be done by performing the following steps:

  1. Create the index page at www/index.html as follows:

    <!DOCTYPE html>
    <html>
    <head>
        <title>My WebRTC file sharing application</title>
    <style type="text/css">
  2. Include the following two JavaScript libraries:

    </style>
        <script type="text/javascript" src="myrtclib.js"></script>
        <script type="text/javascript" src="myrtcadapter.js"></script>
    </head>
    
    <body onLoad="onPageLoad();">
    <div id='status'></div>
  3. Using this object, we will handle the list of files chosen by the customer. Using this list, we will send files one by one to the peer by using the following code:

    <div>
        <input...

Running the application


Now we're ready to test our file-sharing application. This can be done by performing the following steps:

  1. Go to the project's root folder and compile the signaling server using the following command:

    rebar clean
    rebar get-deps
    rebar compile
    
  2. If you're under the Linux box, start the signaling server using the following command:

    erl -pa deps/*/ebin apps/*/ebin -sasl errlog_type error -s rtcserver_app
    
  3. If you're using the Windows box, use the following command to start the signaling server:

    erl -pa deps/cowboy/ebin deps/cowlib/ebin deps/gproc/ebin deps/jsonerl/ebin deps/ranch/ebin apps/rtcserver/ebin -sasl errlog_type error -s rtcserver_app
    
  4. Now, point your web browser to the domain or IP address where the application is accessible. You should see two buttons and a link (URL). In the following screenshot, you can see Chrome and the main page of the application:

  5. Open the link in another web browser or any other computer; you should see a similar page there. I used Firefox...

Summary


We developed a simple peer-to-peer file-sharing application using the WebRTC DataChannel API. Using it, two peers can establish a direct connection between each other and send files to each other. You can choose just one file to transmit or you can select a list of files and they will be transmitted one by one.

You also got a brief practical introduction to the HTML5 File API and learned how to use it to work with the filesystem from JavaScript.

In the next chapter, we will delve deeply into the WebRTC API and develop an application that will be able to stream a prerecorded peer-to-peer video. This application will also be able to share a desktop to the remote peer.

Left arrow icon Right arrow icon

What you will learn

  • Create video conference web services that work without installing plugins or additional thirdparty software
  • Use ICE and STUN to pass through NAT and firewalls
  • Learn how to create and use direct peertopeer data channels to secure exchange data
  • Build a crossplatform signalling server for WebRTC applications
  • Work with user files from JavaScript code using the modern HTML5 File API
  • Install and configure your own TURN/STUN server
  • Integrate your application with a TURN server using authentication
  • Make your application more secure and safe using HTTPS
  • Develop your own secure web applications and services with practical projects

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : May 15, 2014
Length: 176 pages
Edition :
Language : English
ISBN-13 : 9781783983100
Languages :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : May 15, 2014
Length: 176 pages
Edition :
Language : English
ISBN-13 : 9781783983100
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 104.97
Getting Started with WebRTC
€26.99
WebRTC Blueprints
€35.99
WebRTC Integrator's Guide
€41.99
Total 104.97 Stars icon
Banner background image

Table of Contents

5 Chapters
Developing a WebRTC Application Chevron down icon Chevron up icon
Using the WebRTC Data API Chevron down icon Chevron up icon
The Media Streaming and Screen Casting Services Chevron down icon Chevron up icon
Security and Authentication Chevron down icon Chevron up icon
Mobile Platforms Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.2
(9 Ratings)
5 star 66.7%
4 star 11.1%
3 star 11.1%
2 star 0%
1 star 11.1%
Filter icon Filter
Top Reviews

Filter reviews by




Lantre Barr Aug 13, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
WebRTC Blueprints is a hard core guide for building upon previous knowledge of WebRTC. While it's not for newbies, it goes in depth to show you how to get started with your first implementation of WebRTC from the ground up.It gives you step by step instructions using each element of the WebRTC API, to help you bring it all together at the end. WebRTC Blueprints takes it one step further by including mobile into the mix. Mobile still has a long way to go but it a good starting point for those interested in building native applications using WebRTC.Overall, I am pleased with WebRTC Blueprints and recommend it as read after you are familiar with WebRTC.--Lantre Barr, Founder & CEO of Blacc Spot Media - A WebRTC Development Agency in Atlanta, Ga
Amazon Verified review Amazon
Tsahi Levent-Levi Aug 05, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book isn't for those who are new to WebRTC.It is intended for those who have been playing with it a bit, have the basics covered and have written their first "hello world" example with WebRTC already.It takes a brave approach and covers issues that other books dealing with WebRTC today shy away from. This takes it to the cutting edge, which sometimes means the information found in the book would be a bit outdated (WebRTC hasn't finalized its spec yet).That said, this is the only book that handles the installation, configuration and security implications of deploying TURN servers and the use of WebRTC's screen casting technology.
Amazon Verified review Amazon
GSM Aug 13, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This is a great book for web developers wanting to get into the world of audio/video/data streaming using the open source webRTC technology.This is not for the newbie web programmer, and a previous knowledge on "communications" will help you navigating some of the more technical topics of the book (e.g. SDP,...) but the author provides a clear structure and it tries to simplify some of the complexities around streaming audio and video across the web, and it's related technologies.And it also covers mobile apps, which is a plus !
Amazon Verified review Amazon
howlowck Aug 13, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book throws you in the deep end from the get-go. At the end of the first chapter, you will have build a WebRTC application, something that is usually accomplished at the end of a book on WebRTC.The book uses Erlang, a language that only a percentage of web developers know about, to build its webRTC servers. So if you are only curious about webRTC, or only care about consuming webRTC on the client side, then this book is probably not right for you. If you don't know Erlang, you'd have to be comfortable with the language to know the server side. However for the concepts that the book explains, the content is very VERY thorough and does not keep any gaps in its explanation.
Amazon Verified review Amazon
ACE Jul 22, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
It's an excellent book on how to actually build your own WebRTC Server & Service from scratch. This book will put everything together if you have or have-not read any of the other WebRTC books. This book covers all of the required components to make it work. The best part is that everything is Open Source.I would re-arrange some of the chapters though. For example, the Introductory sections in Chapter 2, I believe, should be at the beginning of Chapter 1.The downside to this book is that the author assumes a certain level of Unix/Linux expertise. This book is definitely not for anyone at the beginner level. But, if you know what WebRTC or HTML5 is, then you should have some Unix experience under your belt. If its been awhile, then this is a good way to get reacquainted.I highly recommend this book. Again, it really does cover everything you'll need and how to do it. Depending on how much time you've got and your level of expertise, you could actually have a WebRTC Service up and running in a day to a week.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.