@miukyo/ytlc
v0.1.3
Published
Unofficial JavaScript library for reading YouTube live chat via InnerTube
Maintainers
Readme
@miukyo/ytlc Library Guide
@miukyo/ytlc is a TypeScript/JavaScript library for receiving YouTube live chat events.
YTLiveChat Service API
This library is centered on the YTLiveChat service.
1. Create service instance
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().
2. Subscribe to events
livechat.on("chatReceived", ({ chatItem }) => {
console.log(chatItem.author.name);
});Return type:
livechat.on(...)returnsthis(chainable).
3. Start monitoring
await livechat.start({ handle: "@YouTubeChannelHandle" });Required start options: at least one of:
handlechannelIdliveId
Return type:
start(...)returnsPromise<void>.
4. Stop / stream / dummy helpers
stop(reason?)->voidsendDummy(options?)->ChatItemstreamChatItems(...)->AsyncIterable<ChatItem>streamRawActions(...)->AsyncIterable<RawActionReceivedEventArgs>
Type Reference
Import types from the package root:
import type {
ChatItem,
MembershipDetails,
MessagePart,
StartOptions,
SendDummyOptions,
YTLiveChatOptions,
YTLiveChatEvents,
} from "@miukyo/ytlc";Source references:
- Models:
src/types/models.ts - Options:
src/types/options.ts - Events:
src/types/events.ts - Service interface:
src/interfaces/i-yt-live-chat.ts
Most commonly used types:
YTLiveChatOptions: constructor configurationStartOptions: required target (handleorchannelIdorliveId) forstart(...)ChatItem: parsed chat payload emitted inchatReceivedSendDummyOptions: shape forsendDummy(...)
Install
npm install @miukyo/ytlcQuick Start
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" });Stream API
for await (const item of chat.streamChatItems({ liveId: "VIDEO_ID" })) {
console.log(item.author.name);
}Local Dummy Events
Use the same pipeline without network calls:
chat.sendDummy({
mode: "text",
text: "local test message",
authorName: "dev-bot",
});Reconnect Behavior
autoReconnect: enable retry behavior.reconnectMaxAttempts: maximum retry attempts before stopping.reconnectDelayMs: fixed interval between retries.
Common Start Options
handle: channel handle (@...)channelId: channel id (UC...)liveId: direct live video idoverwrite: restart active session
Notes
- This is an unofficial parser over YouTube internal payloads. Schema may change anytime.
Credits
- YTLiveChat by Agash - Reference repo (C#)
