Skip to content

Instantly share code, notes, and snippets.

@deepak2431
Created September 1, 2021 07:48
Show Gist options
  • Save deepak2431/4a04b7a0e8147a432e9d5268b53a2540 to your computer and use it in GitHub Desktop.
Save deepak2431/4a04b7a0e8147a432e9d5268b53a2540 to your computer and use it in GitHub Desktop.
Centrigugo subscription to multiple tokens react
import React, { useEffect, useState } from 'react';
import Centrifuge from 'centrifuge';
const Option = () => {
const [askPriceList, setAskPriceList] = useState([]);
const [askPriceT, setAskPriceT] = useState();
const [connected, setConnected] = useState(0);
useEffect(() => {
const centrifuge = new Centrifuge('wss://stock-api.fnotrader.com/connection/websocket')
centrifuge.on('connect', function (ctx) {
console.log('Connected with client ID ' + ctx.client + ' over ' + ctx.transport);
setConnected(1);
});
centrifuge.on('disconnect', function (ctx) {
console.log('Disconnected: ' + ctx.reason + (ctx.reconnect ? ", will try to reconnect" : ", won't try to reconnect"));
});
centrifuge.connect();
setTimeout(function (){
centrifuge.startBatching();
var channels = ["13211394", "12485890","13722370" ]
let muiltipleask = [];
for (var i in channels) {
var ch = channels[i];
var sub = centrifuge.subscribe(ch, function(message){
console.log("Got message from channel", sub.channel);
var t = message.data.ask.p;
console.log(t);
let tobj = {};
tobj[message.channel] = t;
muiltipleask.push(tobj);
})
}
centrifuge.stopBatching(true);
setAskPriceList(muiltipleask)
console.log(askPriceList)
let chh = askPriceList[0];
if(chh !== undefined) {
if(chh[channels[0]] !== undefined) {
setAskPriceT(chh[channels[0]])
console.log(chh[channels[0]])
}
}
}, 1000)
},)
return (
<div className="option-chart">
<h3>This is option chart API! and the value of ASK Price is {askPriceT}</h3>
</div>
);
}
export default Option;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment