Quickstart: Fetch YouTube live chat
developTo use youtube-chat, import the LiveChat class, instantiate it with either a channelId or a liveId, set up event listeners for chat messages and lifecycle events, and then call .start().
Note: If you provide a channelId, the library will automatically attempt to acquire the current liveId for that channel.
const { LiveChat } = require("youtube-chat");
// Recommended: use channelId to automatically find the current liveId
const liveChat = new LiveChat({ channelId: "CHANNEL_ID_HERE" });
// Listen for chat messages
liveChat.on("chat", (chatItem) => {
console.log(chatItem.author.name, chatItem.message);
});
// Start the fetch loop
const ok = await liveChat.start();
if (!ok) {
console.log("Failed to start, check emitted error");
}
// To stop the loop later:
// liveChat.stop();