Mock Websockets

Fake Websockets implementation

by sonylnagale

HTML

<link rel="stylesheet" href="https://jsfiddle.chartiq.com/chart/css/stx-chart.css">
<link rel="stylesheet" type="text/css" href="https://jsfiddle.chartiq.com/chart/css/stx-chart.css" media="screen" />

<div class="chartContainer" style="width:600px;height:400px;position:relative;"></div>

JavaScript

import { CIQ } from "https://jsfiddle.chartiq.com/chart/js/advanced.js";
import quoteFeedSimulator from "https://jsfiddle.chartiq.com/chart/examples/feeds/quoteFeedSimulator.js";

// start the chart engine
const stxx = new CIQ.ChartEngine({
  container: document.querySelector(".chartContainer")
});

// attach quoteFeed
stxx.attachQuoteFeed(quoteFeedSimulator, {
  refreshInterval: 0
});

// render a chart 
stxx.loadChart("AAPL", function() {
			stxx.setPeriodicity({ timeUnit: 'minute', interval: 1, period: 1 })

setInterval(() =>  {
			let newQuote = CIQ.clone(stxx.currentQuote());
			let sdata = newQuote["Close"] + Math.round((Math.random() - .5) * 100) / 100;

			stxx.updateChartData({
				Last: sdata,
				Date: getRoundedDate(1)
			}, null, { bypassGovernor: true })
		}, 200);
    
});

      

function getRoundedDate(minutes, d = new Date()) {
  let ms = 1000 * 60 * minutes;
  let roundedDate = new Date(Math.round(d.getTime() / ms) * ms);

  return roundedDate
}