@miukyo/ytlc is a TypeScript/JavaScript library for receiving YouTube live chat events.
This library is centered on the YTLiveChat service.
import { YTLiveChat } from "@miukyo/ytlc";
const livechat = new YTLiveChat(options);
Required constructor options: none.
options is optional (YTLiveChatOptions). Most users can start with new YTLiveChat().
livechat.on("chatReceived", ({ chatItem }) => {
console.log(chatItem.author.name);
});
Return type:
livechat.on(...) returns this (chainable).await livechat.start({ handle: "@YouTubeChannelHandle" });
Required start options: at least one of:
handlechannelIdliveIdReturn type:
start(...) returns Promise<void>.stop(reason?) -> voidsendDummy(options?) -> ChatItemstreamChatItems(...) -> AsyncIterable<ChatItem>streamRawActions(...) -> AsyncIterable<RawActionReceivedEventArgs>Import types from the package root:
import type {
ChatItem,
MembershipDetails,
MessagePart,
StartOptions,
SendDummyOptions,
YTLiveChatOptions,
YTLiveChatEvents,
} from "@miukyo/ytlc";
Source references:
src/types/models.tssrc/types/options.tssrc/types/events.tssrc/interfaces/i-yt-live-chat.tsMost commonly used types:
YTLiveChatOptions: constructor configurationStartOptions: required target (handle or channelId or liveId) for start(...)ChatItem: parsed chat payload emitted in chatReceivedSendDummyOptions: shape for sendDummy(...)npm install @miukyo/ytlc
import { YTLiveChat } from "@miukyo/ytlc";
const livechat = new YTLiveChat({ requestFrequency: 1000 });
livechat.on("initialPageLoaded", ({ liveId }) => {
console.log("Monitoring", liveId);
});
livechat.on("chatReceived", ({ chatItem }) => {
const text = chatItem.message
.map((part) => (part.type === "text" ? part.text : part.alt ?? part.url))
.join("");
console.log(`${chatItem.author.name}: ${text}`);
});
livechat.on("errorOccurred", ({ error }) => {
console.error(error.message);
});
await livechat.start({ handle: "@YouTubeChannelHandle" });
for await (const item of chat.streamChatItems({ liveId: "VIDEO_ID" })) {
console.log(item.author.name);
}
Use the same pipeline without network calls:
chat.sendDummy({
mode: "text",
text: "local test message",
authorName: "dev-bot",
});
autoReconnect: enable retry behavior.reconnectMaxAttempts: maximum retry attempts before stopping.reconnectDelayMs: fixed interval between retries.handle: channel handle (@...)channelId: channel id (UC...)liveId: direct live video idoverwrite: restart active session