javascriptnode.jsbackendpug

How to get the data in nodejs console to html


I am using smartapi provided by angelbroking.

I want to make a stock ticker which can display realtime price of stocks like this one

https://www.tickertape.in/screener?utm_source=gads&utm_medium=search&utm_campaign=screener&gclid=Cj0KCQiA8ICOBhDmARIsAEGI6o1xfYgsbvDEB6c2OFTEYRp9e5UDnJxgCyBJJphdKTduZ_EOHCAchpoaAp-WEALw_wcB

I am able to connect to websocket using the sdk provided in documentation but I don't know how to display that data in my html page.

Please suggest if you know how to get the json data from nodejs console to html.

The nodejs code is

let { SmartAPI, WebSocket } = require("smartapi-javascript");


let web_socket = new WebSocket({
    client_code: "P529774",
    feed_token: "0973308957"
});

web_socket.connect()
    .then(() => {
        web_socket.runScript("nse_cm|2885", "cn") // SCRIPT: nse_cm|2885, mcx_fo|222900  TASK: mw|sfi|dp
        web_socket.runScript("nse_cm|2885", "mw")
            /*setTimeout(function() {
                web_socket.close()
            }, 60000)*/
    })

web_socket.on('tick', receiveTick)


function receiveTick(data) {
    console.log("receiveTick:::::", data)
}

The response I get is similar to this :

[{"ak":"ok","task":"mw","msg":"mw"}]
 [{"lo":"1797.55","ts":"ACC-EQ","tp":null,"ltp":"1800.05","ltq":"27","bs":"16","tk":"22","ltt":"31\/08\/2017 11:32:01",
 "lcl":null,"tsq":"76435","cng":"-11.15","bp":"1800.00","bq":"510","mc":"34012.01277(Crs)","isdc":"18.77872
 (Crs)","name":"sf","tbq":"76497","oi":null,"yh":"1801.25","e":"nse_cm","sp":"1800.90","op":"1814.00","c": "1811.20",
 "to":"145093696.35","ut":"31-Aug-2017 11:32:01","h":"1817.55","v":"80391","nc":"- 00.62","ap":"1804.85","yl":"1800.00","ucl":null,"toi":"16654000" }]

The github repo for smartapi nodejs https://github.com/angelbroking-github/smartapi-javascript

The API Docs https://smartapi.angelbroking.com/docs/Introduction


Solution

  • There are many ways, here's two:

    Cache the last message + HTTP polling

    This is not the most efficient solution, but perhaps the simplest. Each time your recieveTick() callback hits, you could save the response message in a global object / collection (cache it). Better yet, you could pre-process the message and therefore just cache whatever info you actually care about in that global collection and save bandwidth on the connection between your frontend HTML and backend.

    Then, add an HTTP endpoint to your backend that serves up the last info relevant to a given ticker. You could use Express.js or some other simple HTTP server library. That way when your frontend calls

    http://<backend_host>:<backend_port>/tickers/<ticker>
    

    Your backend will read from the cached data and serve up the needed data.

    Create your own websocket and forward the data

    This is a better solution, specially if your data providers API has a quick (subsecond) refresh rate. Create your own websocket server that will make a websocket connection with your frontend. Then, when you get a message from the data providers websocket, simply processes it in whatever way you would like (to get it into the format your frontend wants) then forward it to the frontend by using your websocket server. This will also be done within the recieveTick() function.

    There are many websocket tools for nodejs. For help with the websocket stuff check this out https://ably.com/blog/web-app-websockets-nodejs

    Also just a quick note, in your question you said "...how to get the json data from nodejs console to html". This kind of suggests that you would like to write the data to the console, and then read it from the console to html. This isn't the way you should think about it. The console was one destination, and the html is another, both originating from the websocket callback.