TradingView Chat Watcher is a Node.js application that monitors and retrieves chat messages from a specific TradingView chatroom in real-time. It fetches the historical chat data and then listens to the chatroom's WebSocket for any new incoming messages.
Make sure you have Node.js and npm installed on your system. If not, you can download Node.js and npm from here: https://nodejs.org/
- Clone the repository
git clone https://github.com/yourusername/tradingview-chat-watcher.git
- Navigate into the project directory
cd tradingview-chat-watcher
This application has three dependencies:
-
axios: A promise-based HTTP client that works both in the browser and in a node.js environment. It provides a single API for dealing with XMLHttpRequests and node's HTTP interface. You can install it with the command
npm install axios
. -
ws: A simple to use, blazing fast, and thoroughly tested WebSocket client, server, and console for node.js, up-to-date against RFC-6455. You can install it with the command
npm install ws
. -
events: This module is installed by default with Node.js, so you don't need to install it separately. It provides a mechanism to emit and handle events, which is used in the code to create an event emitter to notify when new data arrives.
You can install these dependencies individually using the npm install {dependency-name}
command.
The main script file is app.js
. You can run it with the command node app.js
. This will start the application and it will begin listening for messages in the specified TradingView chat room.
Here's a breakdown of what the script does:
const axios = require('axios');
const WebSocket = require('ws');
const events = require('events');
axios
: A promise-based HTTP client for making requests.ws
: A WebSocket client and server for Node.js.events
: An inbuilt module in Node.js to handle events.
const roomId = "bitcoin_de_DE";
const origin = 'https://de.tradingview.com';
roomId
: The ID of the chatroom you want to monitor.origin
: The website where the chatroom is hosted.
class ChatWatcher {
// ...
}
This class encapsulates all the logic related to monitoring the chatroom.
constructor(roomId){
this.roomId = roomId;
this.data = [];
this.onNewData = new events.EventEmitter();
//Load & Start Listener
this.loadChatHistoryAsync().then(this.listenChat())
}
roomId
: The ID of the chatroom to monitor.data
: An array to store chat messages.onNewData
: An event emitter that fires whenever a new chat message is received.
The loadChatHistoryAsync
method sends a GET request to the TradingView API to fetch the historical chat data.
async loadChatHistoryAsync(){
//...
}
The listenChat
method creates a WebSocket connection to the TradingView server and listens for any new incoming chat messages.
listenChat(){
//...
}
Finally, we create an instance of the ChatWatcher
class and pass the ID of the chatroom we want to monitor.
const watcher = new ChatWatcher(roomId);
watcher.onNewData.on("newData", (newMessage) => {
if(!newMessage){
//Initial load whole Chat
console.log(watcher.GetChat());
}else{
//Only new Messages
console.log(newMessage);
}
});