template.html 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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> or <a download
  21. href="http://cdn.peerjs.com/0.3/peer.min.js">peer.min.js</a></p>
  22. <h3>2. Create the Peer object</h3>
  23. <p>The Peer object is where we create and receive connections.</p>
  24. <pre>var peer = new Peer({key: 'lwjd5qra8257b9'});</pre>
  25. <p>The 'key' we're passing in to the Peer constructor is a PeerServer
  26. cloud API key. You can use ours for now, but you should <a
  27. href="http://peerjs.com/peerserver">sign up for your own free key</a>.
  28. PeerJS uses PeerServer for session metadata and candidate signaling. You can
  29. also <a href="http://github.com/peers/peerjs-server">run your own PeerServer</a> if you don't like the cloud.</p>
  30. <p>We're now ready to start making connections!</p>
  31. <h2>Usage</h2>
  32. <p>Every Peer object is assigned a random, unique ID when it's created.</p>
  33. <pre>peer.on('open', function(id){
  34. console.log('My peer ID is: ' + id);
  35. });</pre>
  36. <p>When we want to connect to another peer, we'll need to know their peer
  37. id. You're in charge of communicating the peer IDs between users of your
  38. site. Optionally, you can pass in your own IDs to the <a href="#peer"><code>Peer</code>
  39. constructor</a>.</p>
  40. <p>Read the <a href="#peer">Peer API reference</a> for complete
  41. information on its <a href="#peer-options">options</a>, methods, <a href="#peeron">events</a>, and <a
  42. href="#peeron-error">error handling</a>.</p>
  43. <h3>Data connections</h3>
  44. <p>Start a data connection by calling <code>peer.connect</code> with the peer ID of the
  45. destination peer. Anytime another peer attempts to connect to your peer
  46. ID, you'll receive a <code>connection</code> event. </p>
  47. <div class="two-col">
  48. <div class="col col-header">Start connection</div>
  49. <div class="col col-header">Receive connection</div>
  50. <div class="col"><pre>var conn = peer.connect('dest-peer-id');</pre></div>
  51. <div class="col"><pre>peer.on('connection', function(conn) { ... });</pre></div>
  52. <div class="clear"></div>
  53. </div>
  54. <p><code>peer.connect</code> and the callback of the
  55. <code>connection</code> event will both provide a
  56. <code>DataConnection</code> object. This object will allow you to send and receive data:</p>
  57. <pre>conn.on('open', function(){
  58. // Receive messages
  59. conn.on('data', function(data){
  60. console.log('Received', data);
  61. });
  62. // Send messages
  63. conn.send('Hello!');
  64. });</pre>
  65. <p>Read the <a href="#dataconnection">DataConnection API reference</a> for complete details on its methods and events.</p>
  66. <h3>Video/audio calls</h3>
  67. <p>Call another peer by calling <code>peer.call</code> with the peer ID of the destination
  68. peer. When a peer calls you, the <code>call</code> event is emitted.</p>
  69. <p>Unlike data connections, when receiving a <code>call</code> event, the call must be answered or no connection is established.</p>
  70. <div class="two-col">
  71. <div class="col col-header">Start call</div>
  72. <div class="col col-header">Answer call</div>
  73. <div class="col"><pre>// Call a peer, providing our mediaStream
  74. var call = peer.call('dest-peer-id',
  75. mediaStream);
  76. </pre></div>
  77. <div class="col"><pre>peer.on('call', function(call){
  78. // Answer the call, providing our mediaStream
  79. call.answer(mediaStream);
  80. });</pre></div>
  81. <div class="clear"></div>
  82. </div>
  83. <p>When calling or answering a call, a MediaStream should be provided. The
  84. MediaStream represents the local video (webcam) or audio stream and can be
  85. obtained with some (browser-specific) version of <a
  86. href="https://developer.mozilla.org/en-US/docs/Web/API/Navigator.getUserMedia"><code>navigator.getUserMedia</code></a>. When answering a
  87. call, the MediaStream is optional and if none is provided then a one-way
  88. call is established. Once the call is established, its <code>open</code> property is
  89. set to true.</p>
  90. <p><code>peer.call</code> and the callback of the <code>call</code> event
  91. provide a MediaConnection object. The MediaConnection object itself emits
  92. a <code>stream</code> event whose callback includes the video/audio stream of the other peer.</p>
  93. <pre>call.on('stream', function(stream){
  94. // `stream` is the MediaStream of the remote peer.
  95. // Here you'd add it to an HTML video/canvas element.
  96. });</pre>
  97. <p>Read the <a href="#mediaconnection">MediaConnection API reference</a> for complete details on its methods and events.</p>
  98. <h2>Common questions</h2>
  99. <h3>What kind of data can I send?</h3>
  100. <p>PeerJS has the <a
  101. href="https://github.com/binaryjs/js-binarypack">BinaryPack</a>
  102. serialization format built-in. This means you can send any JSON type as
  103. well as binary Blobs and ArrayBuffers. Simply send arbitrary data and
  104. you'll get it out the
  105. other side:</p>
  106. <pre>
  107. conn.send({
  108. strings: 'hi!',
  109. numbers: 150,
  110. arrays: [1,2,3],
  111. evenBinary: new Blob([1,2,3]),
  112. andMore: {bool: true}
  113. });</pre>
  114. <h3>Are there any caveats?</h3>
  115. <p>A very low percentage of users are behind symmetric NATs. When two
  116. symmetric NAT users try to connect to each other, NAT traversal is
  117. impossible and no connection can be made. A workaround is to proxy
  118. through what is known as a TURN server. The PeerServer cloud service does
  119. not provide a TURN server. You'll have to find your own. You can pass a TURN
  120. server into the <code>Peer</code> object options. This will allow your PeerJS app to work seamlessly for this situation</p>
  121. <h3>What is the current state of browser compatibility?</h3>
  122. <p>We keep an frequently-updated catalogue of WebRTC compatibility
  123. information and caveats <a href="/status">here</a>.</p>
  124. <h3>What if my peer has not yet connected to the server when I attempt
  125. to connect to it?</h3>
  126. <p>When you try to connect to a peer, PeerServer will hold a
  127. 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>
  128. <h3>What about latency/bandwidth?</h3>
  129. <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>
  130. <p>The latency to establish a connection can be split into two
  131. components:
  132. the brokering of data and the identification of clients. PeerJS has been
  133. designed to minimize the time you spend in these two areas. For
  134. brokering, data is sent through an XHR streaming request before a WebSocket
  135. connection is established, then through WebSockets. For client
  136. identification, we provide you the ability to pass in your own peer
  137. IDs, thus eliminating the RTT for retrieving an ID from the server.</p>
  138. <h3>More questions?</h3>
  139. <p><a
  140. href="https://groups.google.com/forum/?fromgroups#!forum/peerjs">Discuss
  141. PeerJS on our Google Group.</a><br><br></p>
  142. </section>
  143. <header class="left">
  144. <h2>API Reference<a class="hide icon">&laquo;</a><a class="show icon">&raquo;</a></h2>
  145. </header>
  146. <header class="right">
  147. <h2>Getting Started</h2>
  148. </header>
  149. <section class="api">
  150. {{{html}}}
  151. </section>
  152. </body>