youtube-livechat-emitter
v0.1.12
Published
The simple emitter to emit the events of YouTube LiveChat
Maintainers
Readme
youtube-livechat-emitter
The simple emitter to emit the events of YouTube LiveChat.
Getting Started
Install
npm i youtube-livechat-emitterImport and create instance
import { YoutubeLiveChatEmitter } from "youtube-livechat-emitter";
const channelId = "@channelHandle"; // not handle style is also valid.
const timeoutMilliSeconds = 1 * 1000; // polling interval to fetch live chat data
const emitter = new YoutubeLiveChatEmitter(channelId, timeoutMilliSeconds);Set event listeners you want to observe. There are 12 kind of events.
- start
start event will be emitted when emitter starts successfully.
No parameters passed to listener function.
emitter.on("start", () => {
// do something.
});- end
end event will be emitted when emitter stops the job.
No parameters passed to listener function.
emitter.on("end", () => {
// do something.
});- error
error event will be emitted when something wrong caused in emitter's job.
Error parameter passed to listener function.
emitter.on("error", (err: Error) => {
// do something.
});- addChat
addChat event will be emitted when someone chats, do super chat or do super sticker.
LiveChatItem parameter passed to listener function.
You can know what kind of item via type field.
emitter.on("error", (item: LiveChatItem) => {
// do something.
});- removeChat
removeChat event will be emitted when a chat removed.
LiveChatItemId parameter passed to listener function. It is identifier of which LiveChatItem instance.
emitter.on("removeChat", (id: LiveChatItemId) => {
// do something.
});- blockUser
blockUser event will be emitted when streaming owner bans someone.
ChannelId parameter passed to listener function. It is Youtube channel id whose banned.
emitter.on("blockUser", (channelId: ChannelId) => {
// do something.
});- pinned
pinned event will be emitted when chat item pinned by streaming owner.
ChatItemText parameter passed to listener function. It is item which pinned. ChatItemText is one of LiveChatItem.
emitter.on("pinned", (item: ChatItemText) => {
// do something.
});- unpinned
unpinned event will be emitted when pinned item is removed.
Optionally ChatItemText parameter passed to listener function.
emitter.on("unpinned", (item?: ChatItemText) => {
// do something.
});- memberships
memberships event will be emitted when someone becomes member or someone reaches member's milestone.
MembershipItem parameter passed to listener function.
You can distinguish which event occurred by its type field.
emitter.on("memberships", (item: MembershipItem) => {
// do something.
});- sponsorshipsGift
sponsorshipsGift event will be emitted when someone purchases the gift.
SponsorshipsGift parameter passed to listener function.
emitter.on("sponsorshipsGift", (item: SponsorshipsGift) => {
// do something.
});- redemptionGift
redemptionGift event will be emitted when someone consumes gift purchased by others.
GiftRedemption parameter passed to listener function.
emitter.on("redemptionGift", (item: GiftRedemption) => {
// do something.
});- addTicker
addTicker event will be emitted when ticker item fixed to chat window.
TickerItem parameter passed to listener function.
You can distinguish which kind of ticker by its type field.
The ticker items are belows:
- purchased super chat
- purchased super sticker
- someone became new membership
- purchased gift
emitter.on("addTicker", (item: TickerItem) => {
// do something.
});After registering listener functions, call start().
Since this time, emitter starts connection to external server.
emitter.start();If you want to finish this emitter, call close().
emitter.close();Here is a complete sample:
import { YoutubeLiveChatEmitter } from "youtube-livechat-emitter";
const channelId = "@CHANNEL_HANDLE";
const timeoutMilliSeconds = 1 * 1000;
const emitter = new YoutubeLiveChatEmitter(channelId, timeoutMilliSeconds);
emitter.on("error", (err) => {
console.log("Error occurred.", err);
});
emitter.on("addChat", (item) => {
switch (item.type) {
case "text":
console.log("Text", item);
break;
case "superChat":
console.log("SuperChat", item);
break;
case "superSticker":
console.log("SuperSticker", item);
break;
}
});
emitter.start();
setTimeout(() => {
emitter.close();
}, 60 * 1000);