Send an object from server.js to client.js?


#1

I’m new to web development so I need some help with this.

My server.js is a Discord bot. So when it receives a message, I want it to send an object with the message data to client.js so that it could be displayed on the app’s page.

How can I achieve this?


#2

An easy way to do this is to use the event message.js, what that event does is that every time a message gets send into a channel the bot can read, it receives a ping. You can do whatever you want with this. You can store the message, you can let the bot reply to it. But for you, the most useful thing would be to store it in an enmap. And let your client.js check for a new update every 5/10/15 mins. Then, if something updated (a message got stored). Get that message in your client.js and use it for whatever you want.


#3

Sorry for being so late, I just found out about this forum.

To answer your question I’ll need a bit more context but I’ll assume this is the project you’re talking about: pew-bot (can’t create link, am new user)
So there are 2 ways of doing this you could have the bot save the necessary data from the messages and have express show that on it’s webpage somehow. But if you want live message updates without refreshing the site you’ll have to send the message data to the client like you said, this can be accomplished with socket.io. You can simply bind socket.io with express and have the client.js connect with your server.js through a socket. Then server.js can freely send data to the client