template.html 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <head>
  2. <title>PeerJS Documentation</title>
  3. <meta name="viewport" content="width=device-width, maximum-scale=1">
  4. <link href='https://fonts.googleapis.com/css?family=Lato:300,400,700,900' rel='stylesheet' type='text/css'>
  5. <link href="/css/docs.css" rel="stylesheet" type="text/css">
  6. <script type="text/javascript" src="/js/jquery.min.js"></script>
  7. <script type="text/javascript" src="/js/docs.js"></script>
  8. </head>
  9. <body>
  10. <section class="start">
  11. <h1>
  12. <a href="/">PeerJS</a>
  13. <span class="title">docs</span>
  14. </h1>
  15. <p>
  16. <br>PeerJS simplifies peer-to-peer data, video, and audio calls.</p>
  17. <p>This guide will show you the basic concepts of the PeerJS API.</p>
  18. <h2>Setup</h2>
  19. <h3>1. Include the Javascript client</h3>
  20. <p>Add the PeerJS client library to your webpage.</p>
  21. <pre>&lt;script src="https://unpkg.com/peerjs@1.3.1/dist/peerjs.min.js"&gt;&lt;/script&gt;</pre>
  22. <p>If you prefer, you can host it yourself:
  23. <a download href="https://unpkg.com/peerjs@1.3.1/dist/peerjs.min.js">peerjs.min.js</a>, or
  24. <a href="https://github.com/peers/peerjs">fork us on Github</a>.</p>
  25. <h3>2. Create the Peer object</h3>
  26. <p>The Peer object is where we create and receive connections.</p>
  27. <pre>var peer = new Peer();</pre>
  28. <p>PeerJS uses PeerServer for session
  29. metadata and candidate signaling. You can also
  30. <a href="https://github.com/peers/peerjs-server">run your own PeerServer</a> if you don't like the cloud.</p>
  31. <p>We're now ready to start making connections!</p>
  32. <h2>Usage</h2>
  33. <p>Every Peer object is assigned a random, unique ID when it's created.</p>
  34. <pre>peer.on('open', function(id) {
  35. console.log('My peer ID is: ' + id);
  36. });</pre>
  37. <p>When we want to connect to another peer, we'll need to know their peer id. You're in charge of communicating the
  38. peer
  39. IDs between users of your site. Optionally, you can pass in your own IDs to the
  40. <a href="#peer">
  41. <code>Peer</code> constructor
  42. </a>.</p>
  43. <p>Read the
  44. <a href="#peer">Peer API reference</a> for complete information on its
  45. <a href="#peer-options">options</a>, methods,
  46. <a href="#peeron">events</a>, and
  47. <a href="#peeron-error">error handling</a>.</p>
  48. <h3>Data connections</h3>
  49. <p>Start a data connection by calling
  50. <code>peer.connect</code> with the peer ID of the destination peer. Anytime another peer attempts to connect to
  51. your peer ID, you'll receive
  52. a
  53. <code>connection</code> event. </p>
  54. <div class="two-col">
  55. <div class="col col-header">Start connection</div>
  56. <div class="col col-header">Receive connection</div>
  57. <div class="col">
  58. <pre>var conn = peer.connect('dest-peer-id');</pre>
  59. </div>
  60. <div class="col">
  61. <pre>peer.on('connection', function(conn) { ... });</pre>
  62. </div>
  63. <div class="clear"></div>
  64. </div>
  65. <p>
  66. <code>peer.connect</code> and the callback of the
  67. <code>connection</code> event will both provide a
  68. <code>DataConnection</code> object. This object will allow you to send and receive data:</p>
  69. <pre>conn.on('open', function() {
  70. // Receive messages
  71. conn.on('data', function(data) {
  72. console.log('Received', data);
  73. });
  74. // Send messages
  75. conn.send('Hello!');
  76. });</pre>
  77. <p>Read the
  78. <a href="#dataconnection">DataConnection API reference</a> for complete details on its methods and events.</p>
  79. <h3>Video/audio calls</h3>
  80. <p>Call another peer by calling
  81. <code>peer.call</code> with the peer ID of the destination peer. When a peer calls you, the
  82. <code>call</code> event is emitted.</p>
  83. <p>Unlike data connections, when receiving a
  84. <code>call</code> event, the call must be answered or no connection is established.</p>
  85. <div class="two-col">
  86. <div class="col col-header">Start call</div>
  87. <div class="col col-header">Answer call</div>
  88. <div class="col">
  89. <pre>// Call a peer, providing our mediaStream
  90. var call = peer.call('dest-peer-id',
  91. mediaStream);
  92. </pre>
  93. </div>
  94. <div class="col">
  95. <pre>peer.on('call', function(call) {
  96. // Answer the call, providing our mediaStream
  97. call.answer(mediaStream);
  98. });</pre>
  99. </div>
  100. <div class="clear"></div>
  101. </div>
  102. <p>When calling or answering a call, a MediaStream should be provided. The MediaStream represents the local video
  103. (webcam)
  104. or audio stream and can be obtained with some (browser-specific) version of
  105. <a href="https://developer.mozilla.org/en-US/docs/Web/API/Navigator.getUserMedia">
  106. <code>navigator.getUserMedia</code>
  107. </a>. When answering a call, the MediaStream is optional and if none is provided then a one-way call is
  108. established.
  109. Once the call is established, its
  110. <code>open</code> property is set to true.</p>
  111. <p>
  112. <code>peer.call</code> and the callback of the
  113. <code>call</code> event provide a MediaConnection object. The MediaConnection object itself emits a
  114. <code>stream</code> event whose callback includes the video/audio stream of the other peer.</p>
  115. <pre>call.on('stream', function(stream) {
  116. // `stream` is the MediaStream of the remote peer.
  117. // Here you'd add it to an HTML video/canvas element.
  118. });</pre>
  119. <p>Read the
  120. <a href="#mediaconnection">MediaConnection API reference</a> for complete details on its methods and events.</p>
  121. <h2>Common questions</h2>
  122. <h3>What kind of data can I send?</h3>
  123. <p>PeerJS has the
  124. <a href="https://github.com/binaryjs/js-binarypack">BinaryPack</a>
  125. serialization format built-in. This means you can send any JSON type as well as binary Blobs and ArrayBuffers.
  126. Simply send
  127. arbitrary data and you'll get it out the other side:</p>
  128. <pre>
  129. conn.send({
  130. strings: 'hi!',
  131. numbers: 150,
  132. arrays: [1,2,3],
  133. evenBinary: new Blob([1,2,3]),
  134. andMore: {bool: true}
  135. });</pre>
  136. <h3>Are there any caveats?</h3>
  137. <p>A small percentage of users are behind symmetric NATs. When two symmetric NAT users try to connect to each other,
  138. NAT
  139. traversal is impossible and no connection can be made. A workaround is to proxy through the connection through a
  140. TURN
  141. server. The PeerServer cloud service provides a free TURN server. This will allow your PeerJS app to work seamlessly for this situation</p>
  142. <h3>How do I use a TURN server?</h3>
  143. <p>When creating your Peer object, pass in the ICE servers as the config key of the options hash.</p>
  144. <pre>
  145. var peer = new Peer({
  146. config: {'iceServers': [
  147. { url: 'stun:stun.l.google.com:19302' },
  148. { url: 'turn:homeo@turn.bistri.com:80', credential: 'homeo' }
  149. ]} /* Sample servers, please use appropriate ones */
  150. });
  151. </pre>
  152. <h3>What if my peer has not yet connected to the server when I attempt to connect to it?</h3>
  153. <p>When you try to connect to a peer, PeerServer will hold a connection offer for up to 5 seconds before rejecting
  154. it. This
  155. is useful if you want to reconnect to a peer as it disconnects and reconnects rapidly between web pages.</p>
  156. <h3>Why am I unable to connect?</h3>
  157. <p>You could be behind a symmetric NAT, in which case you'll need to set up a TURN server.</p>
  158. <p>Another possible issue is your network blocking port 443, which the PeerServer cloud runs on. In this you must
  159. use your
  160. own PeerServer running on an appropriate port instead of the cloud service.</p>
  161. <h3>What about latency/bandwidth?</h3>
  162. <p>Data sent between the two peers do not touch any other servers, so the connection speed is limited only by the
  163. upload
  164. and download rates of the two peers. This also means you don't have the additional latency of an intermediary
  165. server.</p>
  166. <p>The latency to establish a connection can be split into two components: the brokering of data and the
  167. identification
  168. of clients. PeerJS has been designed to minimize the time you spend in these two areas. For brokering, data is
  169. sent
  170. through an XHR streaming request before a WebSocket connection is established, then through WebSockets. For client
  171. identification, we provide you the ability to pass in your own peer IDs, thus eliminating the RTT for retrieving
  172. an
  173. ID from the server.</p>
  174. <h3>More questions?</h3>
  175. <p>
  176. <a href="https://t.me/joinchat/ENhPuhTvhm8WlIxTjQf7Og">Discuss PeerJS on our Telegram channel.</a>
  177. <br>
  178. <br>
  179. </p>
  180. </section>
  181. <header class="left">
  182. <h2>API Reference
  183. <a class="hide icon">&laquo;</a>
  184. <a class="show icon">&raquo;</a>
  185. </h2>
  186. </header>
  187. <header class="right">
  188. <h2>Getting Started</h2>
  189. </header>
  190. <section class="api">
  191. {{{html}}}
  192. </section>
  193. </body>