{"id":18,"date":"2013-01-12T18:30:00","date_gmt":"2013-01-12T18:30:00","guid":{"rendered":"\/blog\/blog\/Peer_JS_Performace.html"},"modified":"2013-01-12T18:30:00","modified_gmt":"2013-01-12T18:30:00","slug":"peerjs-how-does-it-work","status":"publish","type":"post","link":"https:\/\/mohammedlakkadshaw.com\/blog\/peerjs-how-does-it-work.html","title":{"rendered":"PeerJS How does it work?"},"content":{"rendered":"<p>I was very excited about the latest Web RTC API\u2019s on Peer-To-Peer video calling and file transfer, and when I reading about these new API\u2019s I came accorss a awesome javascirpt plugin PeerJS, which allows to use the p2p data transfer easily.<\/p>\n<p>I decided to use it for my screensharing app <strong>deadsimplescreensharing<\/strong>, becuase I am hosting the server on heroku and I am facing huge latency issues and I thought using peer-2-peer would be perfect to solve my problem, but sadly it didn\u2019t work!<\/p>\n<h3 id=\"the-problem\">The Problem:<\/h3>\n<p>I modified my screensharing app\u2019s chrome extension to work with peerJS and also the client webpage.<br \/>\nThen I launched the localserver to test it, I got a connection with the extension I could see data being transmitted from the extension but not being recived at the client side.<\/p>\n<p>I then tried to send small messages like \u201cHello\u201d, \u201cHi\u201d etc. and I was able to recive them at the client just fine, so I figure out the problem was peerJS was unable to send large files (around 700 KB), and that part totally sucks!<\/p>\n<h3 id=\"the-solution\">The Solution:<\/h3>\n<p>The possible soloution I could think about is converting the data:uri (screensharing fetch\u2019s the current tab image using data:uri) into an array and then dividing the array into smaller chunks and send them.<br \/>\nAt the client side, recieve the chunks, join and assemble them and then display the image.<\/p>\n<p>Here is the code which I used:<\/p>\n<div class=\"highlighter-rouge\">\n<pre class=\"highlight\"><code>var partialArray = []\nvar partitionSize = 1000 \/\/Size of each sub-array\nfor(i=0; i < originalArray.length; i += partitionSize) {\n\tpartitionArray.push(originalArray.slice(i, Math.min( i, partitionSize+1 )))\n}\n<\/code><\/pre>\n<\/div>\n<p>The above code divides a large array into smaller arrays of size 1000 each which can be transmitter over the network.<\/p>\n<h3 id=\"conclusion\">Conclusion<\/h3>\n<p>There is still lot of work that needs to be done in p2p file transfer or there is some serious issue with peerJS, as it cannot send large files and if it could the file transfer speed is very slow.<br \/>\nStill the thought of p2p connection is exiciting and I am wating for this new technology to develope so that we all can build cool applications using it<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was very excited about the latest Web RTC API\u2019s on Peer-To-Peer video calling and file transfer, and when I reading about these new API\u2019s I came accorss a awesome javascirpt plugin PeerJS, which allows to use the p2p data transfer easily. I decided to use it for my screensharing app deadsimplescreensharing, becuase I am [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-18","post","type-post","status-publish","format-standard","hentry","category-nodejs"],"_links":{"self":[{"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/posts\/18","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/comments?post=18"}],"version-history":[{"count":0,"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/posts\/18\/revisions"}],"wp:attachment":[{"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/media?parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/categories?post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohammedlakkadshaw.com\/blog\/wp-json\/wp\/v2\/tags?post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}