Modern JavaScript Applications
上QQ阅读APP看书,第一时间看更新

Terminologies

Before we get into WebRTC and PeerJS, you need to know the meaning of some terms that we are going to use. These terms are discussed in the following sections.

Stream

A stream is a sequence of any kind of data that is made available over time. A stream object represents a stream. Usually, an event handler or callback is attached to the stream object, which is invoked whenever new data is available.

A media stream is a stream whose data is either audio or video. Similarly, a media source is a physical device, file, or something that provides data that is audio or video. A media consumer is also a physical device, API, or something that uses media stream.

Note

WebRTC allows us to retrieve a media stream of physical media sources, such as microphones, webcams, screens, and so on. We will discuss more about it later in this chapter.

Peer-to-peer network model

Peer-to-peer model is the opposite of the client-server model. In the client-server model, the server provides resources to the clients, whereas in peer-to-peer model, every node in the network acts as a server and client, that is, every node provides and consumes resources. Peers in the peer-to-peer model communicate with each other directly.

To establish a peer-to-peer connection, we need a signaling server, which is used for signaling. Signaling refers to the exchange of data by peers that is required to establish a peer-to-peer connection. Data such as session control messages, network configuration, and so on is required to establish a peer-to-peer connection. A signaling server implements a signaling protocol such as SIP, Jingle, or some other protocol.

A model is selected depending on the requirements and resource availability for the application. Let's consider some examples:

  • To build a video chat app, we should use the peer-to-peer model instead of the client-server model. As each node, in this case, is going to produce a lot data (or frames), and send the data to other node in real time, the server requires a lot of networks and other resources, increasing the server running cost. So, the peer-to-peer model is the best option for a video chat app. For example, Skype video chat is based on the peer-to-peer model.
  • To build a text chat app that stores messages in a centralized database, we should use the client-server model as the amount of data that a client produces is not very high and you would also want to store the messages in a centralized database. For example, the Facebook messenger is based on the client-server model.

Note

To establish a peer-to-peer connection using WebRTC, you will need a signaling server, STUN server, and optional TURN server. We will discuss more about it later in this chapter.

Real-time data

Real-time data is the data that needs to be processed and transferred without much delay. For example, video chatting, live analytics, live stock price, live streaming, text chat, live score, online multiplayer game data, and so on are all real-time data.

Real-time data transfer is a difficult task to achieve. The techniques and technologies used for real-time data transfer depend on the amount of data and whether the loss of data during data transfer is tolerable or not. If the real-time data is large, and the loss of data is intolerable, then it requires a lot of resources to achieve real-time data transfer, making it practically impossible to achieve real-time data transfer. For example, while video chatting, every user generates a lot of frames. If some frames are lost, then it is tolerable, therefore in this case, we can use the UDP protocol as a transport layer protocol that is unreliable and also has less overhead than TCP, making UDP very suitable for video chat application.

Note

WebRTC allows us to transfer real-time media stream produced by it using the SRTP protocol. To transfer arbitrary data, it uses the SCTP protocol. We will discuss more about what these protocols are later in this chapter.