Your browser has Javascript disabled. Please enable it to use this site. Hide this warning

  • Blog:

  • Home
  • Ably News
  • Ably Engineering
  • Realtime APIs
  • Hardest Aspects of Realtime Engineering
  •  •  5 min read

    Introducing Channel Rewind

    One of the driving forces behind Ably is to make the complex simple. Take our APIs, which package Ably’s realtime messaging fabric and global cloud network into standardized programmatic interfaces, making it easy for developers to build demanding realtime apps or distribute data streams to other developers. But we’re always working to improve.

    Let me introduce Channel Rewind, an optimization for Ably's Pub/Sub Channels. Rewind simplifies the process of retrieving realtime messages sent over a channel prior to connection. It effectively enables you to connect to a channel, instantly load the state, and subscribe to all future realtime updates with a single API interaction.

    If you want to jump right in, check out the:

    What is Rewind?

    As a pub/sub messaging platform, our APIs enable clients to subscribe to a channel and receive realtime messages from that point onwards. But it's sometimes useful to access messages from the past.

    Let’s take the example of a channel delivering weather updates. Updates might only be published once every 15 seconds. If a client connects to a channel just after a publish, you don’t want them to wait 15 seconds for the next update - you probably want them to see the most recent update so they’re not left hanging.

    Ably has always supported retrieval of previously-sent messages with our History API. In order to get any messages sent on a channel before connection you would need to make an additional History API query once connected. However, this adds additional latency along with complexity, especially for clients not using Ably Client Library SDKs, such as our MQTT or SSE protocol adapters.

    The process of retrieving previously-sent messages until now:

    1. Client makes a channel attach request
    2. Attach request succeeds, messages start arriving in real time
    3. Client makes a history API request for messages sent from a specified point in the past up to the point of the attachment
    4. History request succeeds, and the client has to integrate the results of the history request with the live message stream

    With the Rewind implementation, you can now pass a parameter along with a connection request to specify that you want to see previous messages upon connection. You can effectively load the current state of a channel and request all future updates in a single request. This means one less API call, lower latency and bandwidth usage, and a more seamless experience.

    The process after Rewind:

    1. Client makes a channel attach request that includes a rewind parameter, specifying a point in the past to retrieve messages from
    2. Attach request succeeds, and the backlog of messages arrives immediately afterwards, after which new messages start arriving in real time

    The backlog seamlessly blends in to the live message stream, with no message loss or duplication and all messages arriving in the same order they were sent, just as if you had actually been attached since the requested rewind point.

    How does Rewind work?

    As above, you can now pass a parameter along with a connection request to specify that you want to see new messages upon connection. There are currently two types of parameters:

    • The number of messages to retrieve. For example rewind=10 would provide the last ten messages published on a channel. The limit is 100. If the request goes back far enough it can reach into persisted history (provided you have it enabled).
    var realtime = new Ably.Realtime('xVLyHw.qahRdQ:6pCpxE153FuyKd0y');
    var channel = realtime.channels.get('[?rewind=10]pan-hug-fur');
    channel.subscribe(function(message) {
      alert('Received: ' + message.data);
    });
    var realtime = new Ably.Realtime('xVLyHw.qahRdQ:6pCpxE153FuyKd0y');
    var channel = realtime.channels.get('[?rewind=10s]pan-hug-fur');
    channel.subscribe(function(message) {
      alert('Received: ' + message.data);
    });

    There are three ways to use Channel Rewind:

    • Use the new channel parameters option in the Ably SDKs. While Rewind is available now through our SDKs using the next two listed methods, the params option will come with Deltas and a future version of Ably's SDKs early in 2020. See this section of the Rewind docs for more.
    • Add channel parameters in a channel name itself. This is especially useful for SSE or MQTT as they don’t use an Ably SDK.
    • Query string parameter for HTTP based connections. If using this method it applies to all channels a client is subscribed to. For example, you can add rewind=1 parameter to an SSE stream and all channels on that stream would rewind the first message upon connection.

    Read the docs for more explicit detail on using Rewind.

    Considerations and limitations:

    • Rewind is currently limited to 100 messages so your connections don’t get overwhelmed. If you want to retrieve more than 100 messages (or if you want pagination) you will still need to carry out a history request.
    • If using Rewind you will receive all ‘rewound’ messages before realtime messages. This is the same as Ably’s stream resume feature.

    Some example use cases for Rewind

    As Rewind allows you to instantly load current state and request all future state changes in a single request, the uses are varied. Here are some examples of when you might implement Rewind.

    A channel that holds location state

    A channel providing weather updates may only receive new data once every 15 seconds. A client that connects one second after the most recent update would need to wait for the next update - or carry out a History API request - before receiving any weather information. But with Rewind they can instantly receive the most recent state of that channel - in this context, the most recent weather update.

    Transport also works. For example, a channel that provides the platform number for the next train arrival. Clients receive the latest platform number while also subscribing to any new updates.

    A chat application

    You might have public chat in a multiplayer game. It makes sense to provide a connecting client with 30 seconds of previous messages so they have some context on what’s been happening in-game before they joined.

    Get the latest state of an IoT device

    Let’s say it’s Winter. You’re about to leave work and want to come home to a warm house. You check the temperature of your house using an app connected to a thermostat. In this scenario, you’d want to know the current temperature in your house (or the state of the temperature channel) instantly upon connection.

    Rewind + Deltas

    Rewind was originally part of the work we’re doing to implement data deltas. Data deltas optimize the bandwidth requirements for subscribers by sending changes instead of the entire payload for each message.

    Deltas will arrive early in 2020, along with full functionality of Rewind. Until then, let us know if you want more info on Deltas.

    Get started with Rewind

    To start using Rewind, visit the documentation or tutorials. As always, please get in touch if you have any questions.

    Kieran Kilbride-Singh

    Kieran Kilbride-Singh

    Writer + marketer with enough technical know-how to be dangerous in GitHub repos. He's been writing about tech for five years, first flexing his fingers on topics like interoperability in IoT devices.

    Read More of Ably News