Node JS program to fetch bitcoin price and showing it in real time using socker.io

 File structure - 





package install - 












Server.js 

const express = require('express');
const app = express();
const ccxt = require('ccxt')
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
const { Console } = require('console');
const { BroadcastChannel } = require('worker_threads');
const io = new Server(server);


app.use(express.static('public'));
app.use(express.urlencoded({extended:false}))
app.use(express.json());

app.get('/sa', (req, res) => {
  res.send("sd");

});

io.on('connection', async (fun) => {
    i = 1;
    s = 2000;
    console.log('started')
     console.log(fun.id)
     const exchange = new ccxt.binance()

     // Load the markets
     let  real = 1
     await exchange.loadMarkets()
     fun.on('chat message', async (msg) => {
                 const ticker = await exchange.fetchTicker('BTC/USDT')
             
                // Get the ticker for the BTC/USDT market
                    if(ticker.last < real) {
                        console.log("BTC/USDT PRICE - " + ticker.last + "  " + i + "  price drop")
                    }
                    else if (ticker.last > real ){
                        console.log("BTC/USDT PRICE - " + ticker.last + "  " + i + "  price increase")

                    }
                    real = ticker.last
               
                // Log the last price
                const data = {
                    name: ticker.last,
                    surname:s
                }
               
               
                // console.log("check for dat first" , i)
                // console.log("check ", s)
                i++;    
                s++;
                io.emit('chat message', data);
             
         });
         
         fun.on('endchat_1', function (){
            console.log('ended')
            fun.disconnect(fun.id);
        });
         
         fun.on('chat_2', (msg) => {
            console.log('chat_2')
            for (let i = 0; i < 10000; i++) {
                io.emit('chat_2', msg + "  " + i + " chat_2" );
                // console.log( io.emit('chat_2', msg + "  " + i + " chat_2" ))
            }
        });
     });

 
server.listen(3000, () => {
  console.log('listening on *:3000');
});

index.html file - 

<!DOCTYPE html>
<html>
  <head>
    <title>Socket.IO chat</title>
 
  </head>
  <style>
    body{
      background-color: black;
    }
    h1{
      color: white;
    }
  </style>
  <body>
    <h1>BTC Price</h1>
    <ul id="messages"></ul>
    <ul id="">
        chat_2
        <span id="chat_2">
        </span>
    </ul>
    <form id="form" action="">
      <input id="input" autocomplete="off" /><button>Send</button>
    </form>
  </body>
  <script src="/socket.io/socket.io.js"></script>
  <script>
    var socket = io();
   
   
    var messages = document.getElementById('messages');
    var chat_2 = document.getElementById('chat_2');
    var form = document.getElementById('form');
    var input = document.getElementById('input');
 
    form.addEventListener('submit', function(e) {
      e.preventDefault();
      if (input.value) {
        console.log('er')
        socket.emit('chat message', input.value);
        input.value = '';
      }
    });
    let real = 0;
    const func = () =>{
        socket.emit('chat message', "");
        socket.on('chat message', function(msg) {
          if (msg.name < real) {
            messages.style.color = "red"
            messages.innerHTML =  msg.name;
          }
          else if (msg.name > real) {
            messages.style.color = "green"
            messages.innerHTML =  msg.name;
          }
          real = msg.name
    //   var item = document.createElement('li');
    //   item.textContent = msg;
    //   messages.appendChild(item);
    //   window.scrollTo(0, document.body.scrollHeight);
        // chat_2.innerHTML =  msg.surname;
    // console.log('run')
    });
    }
    const func_2 = async () =>{
        // await socket.emit('endchat_1');
        socket.emit('chat_2', "");
        console.log('chat_2')
        let real = 0;
        socket.on('chat_2', function(msg) {
          if (msg < real) {
            chat_2.innerHTML =  msg;
          }
          else if (msg > real) {
            chat_2.style.color = "green"
            chat_2.innerHTML =  msg;
          }
    });
    }
    setInterval(func,500)
    // func();
    // setInterval(func_2,1000)
  </script>
</html>


Output of the terminal -



















output of the index.html -







Comments

All time Best Post

Write a program that declares a class named Person. It should have instance variables to record name, age and salary. Use new operator to create a Person object. Set and display its instance variables.

IMAGE CRUD OPERATION USING EXPRESS MONGOOSE MULTER

connect websocket to binance api and store t he true value in the db

CRUD APP WITH SORT IN NODE EXPRESS AND MONGODB

express session login system using node express mongodb

how to delete image from the folder directly from a Request (MULTER)

calculate rsi

java program for 1 to 10 print using while loop

How to fetch last one minute volume of BTC in CCXT Nodejs.