NodeJS app to get chat messages from a live stream on YouTube. To get the chat messages from a live stream on YouTube, two steps are required. The first step is to get the LiveChatId. The second step is to get the messages using that LiveChatId. Unfortunately these are two different API's. The --id
argument calls the
API with two parameters: &id calls for a specific video (in this case a live stream) and &part with the part name set to liveStreamingDetails returns some properties of that video. We are interested in the activeLiveChatId.https://www.googleapis.com/youtube/v3/videos
The next API call that needs to be made is to
. This is done with the https://www.googleapis.com/youtube/v3/liveChat/messages
--messages
argument. Again two parameters are added: &part has two values: id and snippet. The value of &liveChatId is the previously obtained activeLiveChatId. The call to this API returns live chat messages for the specified live stream. The messages live in items[].snippet.displayMessage
.
Both API calls also need &key. A (limited) free Google API key can be obtained from https://console.cloud.google.com, creating a Project, adding an API to that Project and then creating an API key for that Project. In this app you need to add your API key to sercet.json
under "apiKey:".
Use:
node livechat.js --id [livestreamid]
To get the LiveChatID from Youtube, where [livestreamid] is the ID that can be found in the YouTube video url:
.https://www.youtube.com/watch?v=[thisisthelivestreamid]
Use:
node livechat.js --messages [livechatid]
To print out all the chat messages. Where [livechatid] the previously obtained LiveChatID.
Node.js is an open-source cross-platform JavaScript run-time environment. To install NodeJS:
sudo apt install nodejs
-or- download from https://nodejs.org/en/
npm is a package manager used to add modules to NodeJS. To install npm:
sudo apt install npm
This is where your GoogleAPI API-key lives.
node-fetch is a required module. After cloning, install using npm install node-fetch