Feature Announcement: Exporting Channels

A chat app generates a lot of data based on user interactions. In some cases, such as when you need to provide chat transcripts of a livestream event to the event organizer, it's essential to have easy access to that data in a flexible format. You can then transform the data into your preferred format for analysis by your organization or a third party.

While it's possible to use Stream Chat's standard client methods for exporting data from your chat channels, it may not always be the best option for large sets of data due to the constraints of pagination, data size, and possible rate limits.

To help you deal with this scenario, we implemented a convenient way for exporting channels that gives you all the data from one or multiple channels in the JSON format. Optionally, you can specify a timeframe for the data you want.

Exporting channels is a server-side operation that requires your API secret. The examples below use the Stream Node SDK (@stream-io/node-sdk); the other server-side SDKs support it as well. The Node SDK requires Node.js 22.12 or later.

Channel Export

To request the data from a channel, you should call the exportChannels method. The parameters messages_since and messages_until are optional. If those are not included, the export will contain all data since the creation of the channel.

import { StreamClient } from "@stream-io/node-sdk";

const serverClient = new StreamClient(apiKey, apiSecret);

const response = await serverClient.chat.exportChannels({
    channels: [{
        type: "livestream",
        id: "white-room", 
        messages_since: new Date("2020-11-10T09:30:00.000Z"),
        messages_until: new Date("2020-11-10T11:30:00.000Z"),
    }],
});

const taskID = response.task_id;

That method will not return the data itself, but an identifier (task_id), which you can use to check on the export's status.

Export Status

After you requested the channel export, you can periodically check the export status using the getTask method to verify that it's complete.

const response = await serverClient.getTask({ id: taskID });

console.log(response.status);       // the status for this task
console.log(response.result);       // the result object, only present if the task is completed
console.log(response.result?.url);  // the link to the JSON export
console.log(response.error);        // if not null the description of the task error

Once the task is complete and there's no error, you can find the entire exported data by accessing the URL in the returned object.

Multiple Channels

It's also possible to export the history from multiple channels at once. The method is the same; you just add more objects containing the channel type and id to the channels array. You can specify up to 25 channels. You can also specify messages_since and messages_until for each of them.

await serverClient.chat.exportChannels({
    channels: [
        {type: "livestream", id: "blue-room"},
        {type: "livestream", id: "red-room"},
        {type: "livestream", id: "purple-room"},
    ],
});

We're happy to bring yet another feature to help you understand your chat users better. For more information about this new feature, see the documentation on exporting channels. If you need help or have specific feedback about this feature, please leave a comment on the documentation page.