With a “learn how” post like this one, as a reader you don’t want a long intro. You just want to get to the point. So simply put, this post will explain how WebRTC and WebSockets can work together to build a chat-based application.
WebRTC gives the web browser the capability to connect users to share video, audio and data content directly, this is known as peer-to-peer communication; this is fantastic but in real world applications, we should do a couple of extra things to have the whole flow fully operational.
It’s like if we want to send a letter, we are free to send it to anybody but in addition to the receiver name, we must know its location to be able to deliver the message (among other things). The same occurs with WebRTC, it opens a window to share media content with other peers, but before that we need to exchange some initial information with the counterpart following a specific process.
In the WebRTC ecosystem the process to exchange information with other peers to establish communication is known as “Signaling” and we can list some common actions to describe that process:
The Signaling process is out of the scope of WebRTC (at least at the beginning), so we need a complementary tool to be in charge of it, here is where Websockets come to the rescue.
There are other options to manage the signaling process, but implementing a WebSocket server is one of the approaches that offer more advantages:
To make it easier to explain how WebSockets works together with WebRTC, we can use the following diagram:
In a nutshell, in order to establish a real time communication, John and William must exchange information that WebRTC needs to connect peers.
The WebSockets server is in the middle for this important task. Once each peer has the information required to sharing multimedia data, the direct communication can now start.
In this post we have taken a look (in high level) at how WebSockets and WebRTC can work together to build real world applications.
This is just the beginning.
If you want to know how to setup your own WebSocket server or see a live demo of these cool technologies, stay tuned for future posts.
Do you need more information on how to have WebRTC work in your web application? Do you need help implementing chat-based features in your next project? We have an experienced team ready & happy to help you out.