I'm using cubejs
with React
. I want to display real time data.
I tried using React lifecycle methods & state , where the <QueryRender>
component is triggered every 2 seconds. But it does not work.
I'm using cubejs
with React
. I want to display real time data.
I tried using React lifecycle methods & state , where the <QueryRender>
component is triggered every 2 seconds. But it does not work.
You can switch transport to Web Sockets:
import cubejs from '@cubejs-client/core';
import WebSocketTransport from '@cubejs-client/ws-transport';
const cubejsApi = cubejs({
transport: new WebSocketTransport({ authentication: CUBEJS_TOKEN, apiUrl: 'ws://localhost:4000/' })
});
And use react hooks subscriptions:
import { useCubeQuery } from '@cubejs-client/react';
const Chart = ({ query }) => {
const { resultSet, error, isLoading } = useCubeQuery(query, { subscribe: true });
if (isLoading) {
return <div>Loading...</div>;
}
if (error) {
return <pre>{error.toString()}</pre>;
}
return <LineChart resultSet={resultSet}/>;
};
To learn more: https://cube.dev/docs/real-time-data-fetch