title: Pubnub.history() paging
body: <p>I'm trying to implement paging for <code>pubnub.history()</code>.  Here is what I have so far:</p>
  
  <p><strong>Attempt 1:</strong>  This gets the <em>first</em> <code>count</code> messages from the beginning of time:</p>
  
  <pre><code>function getHistory(chnl,count, token) {
      var pagesize=10;
      console.log('Starting at ' + token + ', getting ' + count);
      pubnub.history({
          channel : chnl,
          callback : function(m){
              var msgs = m[0];
              var msgcount = msgs.length;
              var startToken = m[1];
              var endToken = m[2];
              for (var i = 0; i &lt; msgcount; i++) {
                  var msg = msgs[i];
                  processMessage(chnl,msg);
              }
              if (count &gt;= pagesize) {
                  getHistory(chnl,count - pagesize,endToken);
              }
          },
          count: pagesize,
          start: token,
          reverse: true
      });
  }
  </code></pre>
  
  <p><strong>Attempt 2:</strong>  This gets the most recent <code>pagesize</code> messages over and over again.  <code>token</code> is the same every time:</p>
  
  <pre><code>function getHistory(chnl,count, token) {
      var pagesize=10;
      console.log('Starting at ' + token + ', getting ' + count);
      pubnub.history({
          channel : chnl,
          callback : function(m){
              var msgs = m[0];
              var msgcount = msgs.length;
              var startToken = m[1];
              var endToken = m[2];
              for (var i = 0; i &lt; msgcount; i++) {
                  var msg = msgs[i];
                  processMessage(chnl,msg);
              }
              if (count &gt;= pagesize) {
                  getHistory(chnl,count - pagesize,startToken);
              }
          },
          count: pagesize,
          end: token,
          reverse: false
      });
  }
  </code></pre>
  
  <p>I need to get the last <code>count</code> messages, <em>ending with the most recent</em>.  How can I modify my function to do this?</p>
  
  <hr>
  
  <p><strong>UPDATE: My new API v4 code:</strong></p>
  
  <pre><code>function getHistory(chnl,count, token) {
      var pagesize=10;
      console.log('Starting at ' + token + ', getting ' + count);
      pubnub.history({
          channel : chnl,
          callback : function(m){
  
          },
          count: pagesize,
          start: token,
          reverse: true
      },function(status, response) {
          var msgs = response.messages;
          var msgcount = msgs.length;
          var startToken = response.startTimeToken;
          var endToken = response.endTimeToken;
          for (var i = 0; i &lt; msgcount; i++) {
              var msg = msgs[i];
              processMessage(msg);
          }
          if (count &gt;= pagesize) {
              getHistory(chnl,count - pagesize,endToken);
          }
      });
  }
  </code></pre>
  
  <p>And here is what it is doing right now if I request 1000:</p>
  
  <blockquote>
  <pre><code>2016-10-25 13:29:59.060 pubnub.js:23 Starting at 14772721032416580, getting 620
  2016-10-25 13:29:59.385 pubnub.js:23 Starting at 14772748031396800, getting 610
  2016-10-25 13:29:59.678 pubnub.js:23 Starting at 14772778027380036, getting 600
  2016-10-25 13:30:00.024 pubnub.js:23 Starting at 14772808029462014, getting 590
  2016-10-25 13:30:00.440 pubnub.js:23 Starting at 14772838027743668, getting 580
  2016-10-25 13:30:00.772 pubnub.js:23 Starting at 14772868026780088, getting 570
  2016-10-25 13:30:01.116 pubnub.js:23 Starting at 14772898023567196, getting 560
  2016-10-25 13:30:01.426 pubnub.js:23 Starting at 14772928025649280, getting 550
  2016-10-25 13:30:01.726 pubnub.js:23 Starting at 14772958026355252, getting 540
  2016-10-25 13:30:02.113 pubnub.js:23 Starting at 14773220007794584, getting 530
  2016-10-25 13:30:02.457 pubnub.js:23 Starting at 14774200653664656, getting 520
  2016-10-25 13:30:02.606 pubnub.js:23 Starting at 14774200653664656, getting 510
  2016-10-25 13:30:02.690 pubnub.js:23 Starting at 14774200653664656, getting 500
  2016-10-25 13:30:02.790 pubnub.js:23 Starting at 14774200653664656, getting 490
  2016-10-25 13:30:02.922 pubnub.js:23 Starting at 14774200653664656, getting 480
  2016-10-25 13:30:03.015 pubnub.js:23 Starting at 14774200653664656, getting 470
  </code></pre>
  </blockquote>
  
  <p>As you can see in this instance, there weren't actually 1000 messages so at <code>13:30:02.467</code> it started getting the same message over and over. I then requested 300 and it worked, but it got the <em>first</em> 300, not the most recent 300.</p>
  
  <p>So two issues still remain:</p>
  
  <ol>
  <li>Get all messages if requested number is more than exist.</li>
  <li>Get <em>most recent</em> x messages. </li>
  </ol>