react-stockcharts icon indicating copy to clipboard operation
react-stockcharts copied to clipboard

How to plug live websocket data

Open itsmemittal opened this issue 6 years ago • 8 comments

Hi, I am new to this library would you please help me how I build live candlestick chart with it.

itsmemittal avatar Oct 10 '18 11:10 itsmemittal

You build your own component which connects to the WS and update the data (setState) and you pass the data to the chart component as props (data=this.state.data).

gairom avatar Oct 10 '18 11:10 gairom

Would you help me please with an example if you have?

itsmemittal avatar Oct 10 '18 11:10 itsmemittal

@itsmemittal When you say "live websocket data", do you mean to be able to have the charts automatically update with new data bars? If so, I'm interested in the same.
(I'm a newbie to web frameworks in general, so any guidance would be appreciate)

moonlight16 avatar Oct 10 '18 19:10 moonlight16

Yes i am looking for same.

itsmemittal avatar Oct 11 '18 09:10 itsmemittal

I can provide a code sample later today

gairom avatar Oct 11 '18 13:10 gairom

Ok

itsmemittal avatar Oct 12 '18 11:10 itsmemittal

Your component should look something like:

connect() {
    this.ws = new WebSocket(url);
    this.ws.onmessage = this.onMessage;
    this.ws.onerror = this.onError;
}

onMessage(message) {
    let data = JSON.parse(message.data);
    this.setState({data: data});
    //if the ws is providing high volume data (multiplt updates per sec) you should consider some 
    //throteling, otherwise setting state multiple time per second will heart you app performance.  
    //React currently doesn't support bulk state changes for non-lifecycle methods so you need to 
    //handle it yourself.
}

render() {
    return (
       <CandlesticksChart data={this.state.data} .../>
    )
}

gairom avatar Oct 13 '18 02:10 gairom

you can check out my profile,dogecoin-react, i did something very similair (also with a websocket)

mlasy avatar Nov 26 '18 12:11 mlasy