Reputation: 33
I was looking to set up a simple Discord command to request a shout-out within my stream. I've set up the alert itself within an HTML file, but I really have no clue where to start in getting Node.js to send the alert to the page.
The code provided is what I'm currently using to display the alert.
This is to display an alert box within OBS.
var audio = new Audio('discord.mp3');
function shoutOut(name){
audio.play();
document.getElementById("shoutout").innerHTML = `${name} bought a shout-out!`;
console.log("it worked");
$("#container").fadeIn(1000);
setTimeout(() => {
$("#container").fadeOut(1000);
}, 5000);
}
I'm just expecting it to display the alert upon usage of a Discord command.
Upvotes: 1
Views: 50
Reputation: 418
If I understand correctly, you want your node server to push an update to your webpage in order to fire your function. In order to do this you would first need to setup websocket communication between your webpage and your node server. You would not use HTTP as it is uni-directional and you may only make requests which send back responses. ( if you wish to use HTTP then you would probably have to poll - check to see if there is an update ever x amount of seconds, which is not recommended). Websockets are bi-directional, meaning webpages can request information but at the same time servers can push information without the webpage making a request, since websockets keep an open communication line between the webpage and the server.
Once you have a websocket connection setup, you can add a listener on your client and when you get a notification you can set up the handler to fire your function.
I would read through this to get a better understanding : https://www.compose.com/articles/redis-pubsub-node-and-socket-io/
Upvotes: 1