DISCLAIMER:
I am not a programmer, but only an enthusiastic copy-paster who shares what he could figure out. It is possible that knowledgeable experts may find some points or formulations erroneous or ridiculous.
This material is for informational purposes only.
A guide on how to connect to chats on the ViewHub site and receive all events from there (follow, unfollow, donation, current viewers). Maybe it will be useful for someone to create self-made widgets or multichat.
How to use
The presented example is compiled in javascript, so you will need nodejs
installed and npm
.
To connect to the chat web site, you will need to additionally install the WS
package, being in the same directory with the script:
npm i ws
User Id
Unfortunately, I couldn’t find an easy way to extract the user_id
that is required to connect to the desired chat.
All my attempts to do this via fetch returns an error with the code 403 (Forbidden).
Therefore, this will have to be done manually. Substitute the name of the channel in
https://vh.live/api/v2/profile /{channelname}
And follow the educated link.
The first line under data -> id is the same user_id.
Script
In addition to the usual text messages from users, events about subscriptions / unsubscriptions, donations (only the amount, without currency), as well as the number of current viewers come at a certain interval.
▶ viewhub.js (click to see the code)
const WebSocket = require("ws");
let user_id = "";
///WS
function vhConnect() {
const viewhub = new WebSocket(
"wss://realtime.vh.live/connection/websocket"
);
//Open WS-connection
viewhub.on("open", function open() {
viewhub.send(
JSON.stringify(
{"connect":{"name":"js"},"id":1}
)
);
viewhub.send(
JSON.stringify(
{"subscribe":{"channel":`user_id:${user_id}`},"id":2}
)
);
console.log("Connected to ViewHub");
});
//Add Listener
viewhub.addEventListener('message', function (event) {
try {
let res = JSON.parse(event.data)
/// PING PONG EXCHANGE
if (event.data === JSON.stringify({})) {
return viewhub.send(JSON.stringify({}));
}
let username = res.push?.pub.data.data.username;
let msg = res.push?.pub.data.data.msg;
let sum = res.push?.pub.data.data.sum;
let viewers = res.push?.pub.data.data.viewers;
///CHAT LOG
if (res.push?.pub.data.data.type === 'msg'){
console.log(`${username}: ${msg}`);
}
//VIEWERS
if (res.push?.pub.data.cmd === 'stream.viewers'){
console.log(`Current Viewers: ${viewers}`);
}
///FOLLOW
if (res.push?.pub.data.data.type === 'subscribe'){
console.log(`${username} is now following!`);
}
///UNFOLLOW
if (res.push?.pub.data.data.type === 'unsubscribe'){
console.log(`${username} unfollowed`);
}
///DONATION
if (res.push?.pub.data.data.type === 'donate'){
console.log(`${username} donated ${sum} USD and said: ${msg}`);
}
} catch (error) {
if (error.toString().includes("SyntaxError: Unexpected token")){
return;
}
return console.log(error);
}
});
//On error
viewhub.on("error", console.error);
//On close
viewhub.on("close", (reason) => {
console.log(`Disconnected from ViewHub with code ${reason}`);
viewhub.close()
console.log("Trying to reconnect..")
vhConnect();
});
}
vhConnect();
If this material is useful to you and you have the opportunity,
then support the author and the site with a small tip:
https://hipolink.me/mikulski/tips
Thanks💛