template.html 9.3 KB

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