How to get Pubnub JS history messages

120 Views Asked by At

I am currently trying to get the history of a channel in js, here is my current code:

Send message

function sendMessage(text){
    box=$('box')
    box.innerHTML = ('' + text).replace(/[<>]/g, '') + '<br>' + box.innerHTML
}

Here is my current code for getting history

pubnub.fetchMessages(
    {
      channels: ['ch-1'],
      end: '15343325004275466',
      count: 20
    },
    (status, response) => {
      sendMessage(response.messages[0])
    }
);

But it is not working. can someone please help

1

There are 1 best solutions below

3
Darryn Campbell On

Please try this syntax:

pubnub.fetchMessages( // Get the last 10 messages sent in the chat.
{
   channels: ['ch-1'],
   count: 10,
},
function (status, response) {
   if (response.channels[channel] && channel in response.channels) {
      response.channels[channel].forEach((message) => {
         console.log(message);
      });
   }
});

Another possibility is you have persistence disabled in your admin dashboard (or the period of retention is set too short so you have no stored messages to retrieve)