backend for twitch chat widgets

This commit is contained in:
ari melody 2026-09-02 22:32:45 +01:00
parent 09c11f11a1
commit 28b9492cb3
Signed by: ari
GPG key ID: 60B5F0386E3DDB7E
11 changed files with 725 additions and 94 deletions

0
twitch/public/chat.css Normal file
View file

38
twitch/public/chat.js Normal file
View file

@ -0,0 +1,38 @@
const chatContainer = document.getElementById("chat");
async function sync() {
let eventSource;
try {
eventSource = new EventSource("/twitch/sse/chat");
} catch (err) {
console.error("Failed to connect to event source", err);
return;
}
eventSource.addEventListener("error", () => {
eventSource.close();
console.error("Connection lost, or an error has occurred.");
console.log("Attempting to reconnect...");
setTimeout(() => {
sync();
}, 1000);
});
eventSource.addEventListener("chat", event => {
console.log(`[${chat}] ${event.data}`);
const messageEl = document.createElement("p");
messageEl.innerText = event.data;
chatContainer.appendChild(messageEl);
});
eventSource.addEventListener("open", () => {
console.log("Connected.");
});
}
function twitchEmoteURL(emoteID) {
return `https://static-cdn.jtvnw.net/emoticons/v2/${emoteID}/default/dark/1.0#e=0`
}
sync();