Showing posts with label P2P. Show all posts
Showing posts with label P2P. Show all posts

Monday, January 6, 2025

What is WebRTC and How Does It Work?

 WebRTC or Web Real-Time Connection is an open-source project that allows real-time communication between devices and web browsers, You may confuse it with WebSockets, which also allow users to communicate in real-time. But what is the difference? Is WebRTC a websocket? What is WebRTC and why is it used in many places in place of a web socket? This is what we're gonna learn today.


WebSocket vs WebRTC

Feature WebSocket WebRTC
Communication Model Client-Server Peer-to-Peer
Primary Use Real-time data exchange between client and server Real-time audio, video, and data between peers
Latency Low, but depends on server routing Generally lower due to direct P2P connection
Data Types Text and binary Media streams (audio/video) and data channels
Setup Complexity Relatively simple More complex (requires signaling, NAT traversal)
Server Requirements Requires a server to manage connections and relay messages An initial signaling server is needed, but media/data flows directly between peers

Use Cases comparison:

Both of them have different use cases that they are designed for, and since they're not using the same connection mode;

WebSocket is mostly used for:

  1. Chat Applications: Real-time messaging between users.
  2. Live Notifications: Instant updates like stock tickers or social media feeds.
  3. Online Gaming: Real-time game state synchronization.
  4. Collaborative Tools: Real-time document editing or whiteboards.

While WebRTC is used for:

  1. Video Conferencing: Platforms like Zoom or Google Meet leverage WebRTC for real-time video and audio.
  2. Voice Calls: VoIP applications using browsers.
  3. P2P File Sharing: Direct transfer of files between users.
  4. Interactive Gaming: Real-time multiplayer games requiring low latency.

When to Use Each Technology

Use WebSocket when:
  1. You need real-time communication between a client and a server.
  2. Implementing features like live chat, real-time dashboards, or notifications.
  3. The communication does not involve high-bandwidth media streams.
Use WebRTC when:
  1. You require real-time peer-to-peer media communication (audio/video).
  2. Building applications like video conferencing, live streaming, or P2P file sharing.
  3. You want to reduce server load by handling media directly between clients.
 

What is WebRTC anyway?

Okay, so to explain it simply, it's a technology that provides a collection of APIs and protocols that allow for Peer-to-peer (P2P) exchange of audio, video, and data in general. It was mainly designed to handle video, audio, and generally heavy real-time communication, unlike WebSockets that are used for mostly text, and binary, and are not as frequent. 

synchronizing video or audio online, particularly on a server, can be resource-consuming due to the potential for high traffic from millions of users. To address this issue, WebRTC was developed. With WebRTC, a server is only needed for the initial signaling process, while the actual communication occurs through Peer-to-Peer connections. I've talked about P2P more in this What is Peer-to-peer (P2P) network? if you're interested.

 Key WebRTC APIs are:

  1. MediaStream API (getUserMedia): For capturing audio and video from local devices.
  2. RTCPeerConnection API: For managing connections between peers.
  3. RTCDataChannel API: For exchanging arbitrary data between peers.

Why would you use WebRTC instead of Websockets?

WebRTC's Main purpose in its life is to handle real-time audio and video streams, it has built-in support for codecs, synchronization, and media quality adjustments. While WebSockets are not optimized for media. In addition to that, Peer2Peer communication allows for lower latency and low resource consumption. 

On a WebSocket, as I mentioned earlier, each bit and frame of a video will have to pass through the server, which is very inefficient. WebRTC offers data channels that are optimized for real-time data transfer, with lower latency than Websockets. While WebSockets are capable of handling real-time data, it's not to the level that WebRTC was built for.

Conclusion

In conclusion, You may need to use them together, for example, you need to signal the WebRTC to start the data exchange and establish the peer-to-peer connection. You could even use them in parallel, WebRTC handles the video/audio or file transfer while WebSockets handles anything that is not that. that's all there is to it. I guess, Thank you for reading. You can check out official docs whenever you think you need to get in-depth in it.

What is Peer-to-peer (P2P) network?

 P2P or Peer to Peer is a distributed network architecture where connected devices, and peers, act as a server and client, instead of data being sent to the server and then processed back down to you, it's decentralized, meaning that you and all the peers that are connected with you share the resources directly with each other, allowing for fast and easy connection that requires has no middle man or authority.

Example of Peer to Peer
Source: Wikipedia

Examples of P2P

Peer-to-peer is often used in illegal activities like sharing pirated games/movies or sometimes it's used because of the speed, sometimes the servers can limit the download speed of any app but with peer, you can reach out to any device that has the files you need and the other device will send it back to you as fast as it can. No server or third party can see what that file is or limit the speed except your ISP; Examples of P2P are:

  1. BitTorrent: A popular file-sharing protocol that enables efficient distribution of large files.
  2. Skype: Uses P2P technology for voice and video calls, enhancing call quality and reducing latency.
  3. Bitcoin: A decentralized cryptocurrency network that relies on P2P principles for transactions and validation.

What are the types of P2P?

There are multiple types of Peer to Peer connections, and each has its own features:

The Unstructured P2P Networks; In this network, peers connect randomly with no organization at all, this type of connection is obviously easy to build but has some efficiency issues for content delivery.

On the other hand, Structured P2P Networks; Are the opposite of the latter, peers are structured and each of them has a specific task or portion of the network, allowing for more efficient content location.

Finally, there is the Hybrid P2P Networks which are a combination of both P2P and client-server models, They typically use a centralized server for specific functionality while maintaining peer-to-peer interactions for others.

What made P2P standout

In summary, peer-to-peer (P2P) connections offer significant benefits for both end users and developers. They enable low-latency communication, improving the overall user experience, reducing server workload, and allowing servers to allocate resources to other tasks more efficiently. This makes P2P particularly valuable in applications where minimal delay is crucial, such as video conferencing, online gaming, and voice calls.

Additionally, P2P networks excel in scenarios requiring high-speed data transfer. Unlike centralized servers, which could struggle under the burden of managing massive data volumes—such as transmitting hundreds of gigabits per second to thousands of users—P2P connections distribute the workload across participants, ensuring smoother operations. They are also inherently more resilient to censorship, server outages, and network disruptions, offering a decentralized and robust alternative to traditional systems.

Furthermore, P2P technology promotes scalability by leveraging the resources of connected devices, making it a cost-effective solution for developers. It can also enhance privacy, as direct communication between peers minimizes reliance on intermediaries. These features position P2P as a versatile and future-proof technology in the evolving digital landscape.