ModdyLP
ModdyLP

Reputation: 13

Get Chat Messages in Reverse Chronological Order

I want to get the newest Chat Messages from the Live Streaming API. But I always get the oldest first. Can I get it in an Inverse Order? Is there a property I can set to achieve this?

Here is some example code:

    if ($_GET['action'] == "listchatmessages")
    {
        $htmlBody .= '<h1>Chatnachrichten</h1>';
        $broadcastsChatResponse = $youtube->liveChatMessages ->listLiveChatMessages (
        $broadcastItem['snippet']['liveChatId'], 'snippet');
        $myfile = fopen("chatmessages.txt", "w") or die("Unable to open file!");
        $count = 0;
        if ($_GET['search'] != "")
        {
        if (!empty($broadcastsChatResponse['items']))
            {
                foreach ($broadcastsChatResponse['items'] as $broadcastChatItem)
                {
                    if ($count < 200)
                    {

                        $broadcastsDetailsChatResponse = $youtube->liveChatMessages ->listLiveChatMessages (
                        $broadcastChatItem['snippet']['liveChatId'], 'authorDetails');
                        $broadcastDetailsChatItem = $broadcastsDetailsChatResponse['items'][0];

                            if ((preg_match('/'.$_GET['search'].'/',$broadcastChatItem['snippet']['textMessageDetails']['messageText'])) OR $_GET['search'] == 'all')
                            {
                                $message = $broadcastChatItem['snippet']['authorChannelId'].'_'.$broadcastDetailsChatItem['authorDetails']['displayName'].'_'.$broadcastChatItem['snippet']['textMessageDetails']['messageText']."\n";
                                $htmlBody .= '<li> Author: <a href="'.$broadcastChatItem['snippet']['authorChannelId'].'">'.$broadcastDetailsChatItem['authorDetails']['displayName'].'</a> schrieb: '.$broadcastChatItem['snippet']['textMessageDetails']['messageText'].'</li>';
                                $count++;
                                fwrite($myfile, $message);
                            }
                    }
                    else
                    {
                        break;
                    }
                }


                $htmlBody .= '<h4> Alle Chatnachrichten empfangen (Anzahl der teilnehmenden Nahcrichten: '.$count.')</h4>';
            }
            else
            {
                $htmlBody .= '<p>Der Stream scheint nicht online zu sein</p>';
            }
        }
        else
        {
            $htmlBody .= 'Kein Suchwort. Nutze all um alle Nachrichten zu nehmen';
        }

        fclose($myfile);
    }

Upvotes: 1

Views: 785

Answers (1)

JAL
JAL

Reputation: 42479

There does not seem to be a parameter you can pass into your request to the liveChatMessages/list endpoint to return the newest messages first.

You will have to download all of the messages and sort them by the ISO 8601 formatted date under each chat message's snippet.publishedAt.

I would also recommend you file a enhancement ticket to Google here if you would like this feature implemented in the API.

Upvotes: 2

Related Questions