Last active
October 5, 2021 04:08
-
-
Save timcole/b8f24ff545fca28c1ee4c80aec5e635f to your computer and use it in GitHub Desktop.
Twitch Activity Alerts Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PubSub { | |
private url: string = "wss://pubsub-edge.twitch.tv"; | |
protected connection: WebSocket; | |
private heartbeat: number; | |
private topic: string; | |
constructor(channel: number) { | |
this.topic = `dashboard-activity-feed.${channel}`; | |
this.connection = new WebSocket(this.url); | |
this.connection.onopen = this.onOpen.bind(this); | |
this.connection.onmessage = this.onMessage.bind(this); | |
this.connection.onclose = this.onClose.bind(this); | |
} | |
private sendPing(): void { | |
this.connection.send(JSON.stringify({ type: "PING" })); | |
} | |
private listen(): void { | |
this.connection.send( | |
JSON.stringify({ | |
type: "LISTEN", | |
data: { topics: [this.topic] } | |
}) | |
); | |
} | |
private onOpen(): void { | |
console.log("connection opened"); | |
this.sendPing(); | |
this.listen(); | |
this.heartbeat = setInterval(this.sendPing.bind(this), 1000 * 120); | |
} | |
private onClose(): void { | |
console.warn("connection closed"); | |
clearInterval(this.heartbeat); | |
} | |
private sendAlert(title: string, message: string): void { | |
console.log("ALERT!", title, message); | |
} | |
private onMessage({ data: msg }: any): void { | |
let title: string = ""; | |
let message: string = ""; | |
msg = JSON.parse(msg); | |
if (msg.type === "MESSAGE" && msg.data.topic === this.topic) { | |
let data = JSON.parse(msg.data.message); | |
switch (data["type"]) { | |
case "follow": | |
title = `New Follower!`; | |
message = data["follower"]["display_name"]; | |
break; | |
case "subscription": | |
case "prime_resubscription_sharing": | |
case "resubscription_sharing": | |
title = `New Subscriber! - ${ | |
data["subscription_cumulative_tenure_months"] | |
} Months`; | |
message = data["subscriber"]["display_name"]; | |
break; | |
case "raiding": | |
title = `${data["raider"]["display_name"]} started raiding`; | |
message = `welcome ${data["raiding_viewer_count"]} viewers!`; | |
break; | |
case "host_start": | |
title = `${data["host"]["display_name"]} started raiding`; | |
message = `asd`; | |
break; | |
case "bits_usage": | |
title = `${data["bits_user"]["display_name"]} cheered!`; | |
message = `${data["bits_amount"]}`; | |
break; | |
default: | |
break; | |
} | |
if (title !== "") this.sendAlert(title, message); | |
} | |
} | |
} | |
export default PubSub; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import PubSub from "./pubsub"; | |
new PubSub(37402112); // 37402112 = shroud |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment