Search code examples
iosswift2pubnub

PubNub Replay API Example for iOS


I'm trying to play back PubNub messages in real time as suggested by their Replay functionality, however the only examples I can find online just relate to obtaining a static history of messages over a certain timeframe.

Does anyone know of an online tutorial actually dealing with the Replay API request (rather than History API) or can give some sample code here (Swift if possible)?


Solution

  • Custom PubNub Replay Implementation

    The replay feature was only available in the PubNub Node/JavaScript SDK and has been deprecated (it will not be part of the v4 SDKs) because it is not in demand. You can replicate this feature on your end if you require it. Most likely you would want to do this from your server and not your client app but you can do this from your client, too.

    Using the history API (Swift SDK for your client implementation), you will retrieve as many of the messages as needed paging through storage since history only returns 100 messages per call (new features coming in the future to simplify iterative storage paging).

    Here is a high level design, from your server, to implement your own replay feature:

    1. On your server get history from the desired channel – you can only get 100 messages at a time so you may have to page through the messages to get more than 100.
    2. Create a new channel to replay the message on and let your clients know what that channel name is and have them subscribe to it. You might publish a message that has the replay channel name on another channel that those clients are already subscribed to.
    3. Once the clients are subscribed to this replay channel, you can proceed with replaying the messages.
    4. Starting with the oldest message that you retrieved using history, publish it then sleep for the amount of time of the difference between the message timetoken just published and the timetoken of the next message to be published.