Peer-to-Peer Guide

Why Peer-to-Peer?

A secure Peer-to-peer infrastructure means that users get each other’s messages faster than with regular server polling while simultaneously reducing your server’s load. After you’ve connected users in a BlueCam private room, sending data in real time between the users is as easy as calling: sendMessage. BlueCam takes care of delivering messages and prioritizes to use the fastest method, WebRTC, but for robustness will fall back proxying the data through your web server to ensure all messages are delivered. Video and audio chat in BlueCam is always directly peer-to-peer meaning your video chat apps can scale massively.

Sending Data With onMessage(type, data)

sendMessage takes two parameters: type(string) and data (string or object) and passes this to the other client:
sendMessage("changedDrawing", {"circles":[], "rectangles":[], "triangles":[], "bgColor":"blue"});
sendMessage("shareLink", "");

Message Handling

When calling JoinRoom in Javascript the initiate video chat, the onMessage option allows the developer to install custom handlers. You'll usually want to handle messages according to their type property
	onMessage: function(type, data) {
		if(type == "sharedLink") {
			//Custom message handlers for peer-to-peer communication
			alert("A link was shared with you to: " + data);
	roomNotFoundCallback: function() {
Remember: The onMessage code is run on the browser of the recipient of the message.

Why Does Peer-to-Peer Matter

Like the Chess Grandmaster, the Web Developing Guru knows that they need to protect their server (The King) at all costs. This means utilizing resources as efficiently as possible to account for as much scaling as could be necessary. It is important to ask yourself how you would handle a thousand users, a ten thousand users or even a millions users should they all try to access your site at the same time. If you need to perform a significant amount of computational work to service each user, it is usually necessay to scale horizontally (add more servers) and ideally do so when still in the design phase so the whole system doesn't need to be rewritten right when your system is succeeding at achieving critical mass.
Shuttling heavy data(video/ audio/ text) between the users via your server(s) is expensive on your computational and network resources leaving less power to handle other system aspects.
Peer-to-Peer helps with protecting your server in various ways. Right off the bat, WebRTC RTCPeerConnection does all of the heavy lifting of securly exchanging user data without your server's involvement. This is a tremenous accomplishment in and of iteself.
It's also important to think about the path and distance that data has to travel on its path between two users in chat. Imagine your server is in Dallas, TX and two of your customers would like to video chat. User A is located in Dubai, United Arab Emirates and User B in Abu Dhabi. If we had to shuttle the data back and forth via our server, the data would travel in a path like so:
Peer-to-Peer reduces the distance tremendously, allowing for must faster, higher definition video/ audio chat and data exchange.