Real-Time Yahoo Finance Stock data.
This module opens a Websocket connection with Yahoo for reliable, fast, and lightweight market data.
const StockSocket = require("stocksocket");
StockSocket.addTicker("TSLA", stockPriceChanged);
function stockPriceChanged(data) {
//Choose what to do with your data as it comes in.
console.log(data);
}
This is a Node.js module available through the npm registry.
If this is a brand new project, make sure to create a package.json
first with
the npm init
command.
Installation is done using the
npm install
command:
$ npm install stocksocket
- Yahoo Finance uses Websockets to transfer stock data when you open their page on your browser.
- This module leverages that functionality by opening its own direct WebSocket connection with Yahoo (skipping the need for the browser in the process).
- As a result, this module is far more lightweight and quick than previous versions of this module (which used Puppeteer and inefficient web-scraping).
Start data stream for a specific ticker
var stockticker = "TSLA";
StockSocket.addTicker(stockticker, stockPriceChanged);
function stockPriceChanged(data) {
console.log(data);
}
stockticker (type: String
)
String object containing a stock ticker to be added.
callback (type: Function
)
Callback Function that receives each price update
Start data stream for an array of tickers
var stocktickers = ["TSLA", "NNDM", "AAPL", "MARA"];
StockSocket.addTickers(stocktickers, stockPriceChanged);
function stockPriceChanged(data) {
console.log(data);
}
stocktickers (type: Array
)
Array of string objects containing the stock tickers
callback (type: Function
)
Callback Function that receives each price update
Stop data stream for a specific ticker.
var stockticker = "TSLA";
StockSocket.removeTicker(stockticker);
stockticker (type: String
)
String object containing a stock ticker to be added.
Stop data stream for various tickers
var stocktickers = ["TSLA", "NNDM", "AAPL", "MARA"];
StockSocket.removeTickers(stocktickers);
stocktickers (type: Array
)
Array of string objects containing the stock tickers to be removed.
Stop data stream for all tickers
StockSocket.removeAllTickers();