template.html 9.3 KB

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