DevExtreme v26.1 is now available.

Explore our newest features/capabilities and share your thoughts with us.

Your search did not match any results.

React Data Grid - SignalR Service

This example demonstrates real-time data update in a DataGrid bound to a SignalR service. Access to the service is configured in a CustomStore.

The CustomStore fetches the remote dataset at launch and keeps its local copy. Whenever the remote dataset changes, the server calls a client-side function that updates the local copy of the dataset (updateStockPrices in this demo). This function uses the store's push(changes) method.

For server-side configuration, refer to the ASP.NET MVC version of this demo.

Backend API
import React, { useEffect, useState } from 'react'; import { HubConnectionBuilder, HttpTransportType } from '@aspnet/signalr'; import { CustomStore } from 'devextreme-react/common/data'; import DataGrid, { Column } from 'devextreme-react/data-grid'; import PriceCell from './PriceCell.tsx'; import ChangeCell from './ChangeCell.tsx'; const App = () => { const [connectionStarted, setConnectionStarted] = useState(false); const [dataSource, setDataSource] = useState<CustomStore | undefined>(undefined); useEffect(() => { const hubConnection = new HubConnectionBuilder() .withUrl('https://js.devexpress.com/Demos/NetCore/liveUpdateSignalRHub', { skipNegotiation: true, transport: HttpTransportType.WebSockets, }) .build(); const store = new CustomStore({ load: () => hubConnection.invoke('getAllStocks'), key: 'symbol', }); hubConnection .start() .then(() => { hubConnection.on('updateStockPrice', (data) => { store.push([{ type: 'update', key: data.symbol, data }]); }); setConnectionStarted(true); setDataSource(store); }); }, []); return ( <div> {connectionStarted && ( <DataGrid id="gridContainer" dataSource={dataSource} showBorders={true} repaintChangesOnly={true} highlightChanges={true} > <Column dataField="lastUpdate" dataType="date" width={115} format="longTime" /> <Column dataField="symbol" /> <Column dataField="price" dataType="number" format="#0.####" cellRender={PriceCell} /> <Column dataField="change" dataType="number" width={140} format="#0.####" cellRender={ChangeCell} /> <Column dataField="dayOpen" dataType="number" format="#0.####" /> <Column dataField="dayMin" dataType="number" format="#0.####" /> <Column dataField="dayMax" dataType="number" format="#0.####" /> </DataGrid> )} </div> ); }; export default App;
import React, { useEffect, useState } from 'react'; import { HubConnectionBuilder, HttpTransportType } from '@aspnet/signalr'; import { CustomStore } from 'devextreme-react/common/data'; import DataGrid, { Column } from 'devextreme-react/data-grid'; import PriceCell from './PriceCell.js'; import ChangeCell from './ChangeCell.js'; const App = () => { const [connectionStarted, setConnectionStarted] = useState(false); const [dataSource, setDataSource] = useState(undefined); useEffect(() => { const hubConnection = new HubConnectionBuilder() .withUrl('https://js.devexpress.com/Demos/NetCore/liveUpdateSignalRHub', { skipNegotiation: true, transport: HttpTransportType.WebSockets, }) .build(); const store = new CustomStore({ load: () => hubConnection.invoke('getAllStocks'), key: 'symbol', }); hubConnection.start().then(() => { hubConnection.on('updateStockPrice', (data) => { store.push([{ type: 'update', key: data.symbol, data }]); }); setConnectionStarted(true); setDataSource(store); }); }, []); return ( <div> {connectionStarted && ( <DataGrid id="gridContainer" dataSource={dataSource} showBorders={true} repaintChangesOnly={true} highlightChanges={true} > <Column dataField="lastUpdate" dataType="date" width={115} format="longTime" /> <Column dataField="symbol" /> <Column dataField="price" dataType="number" format="#0.####" cellRender={PriceCell} /> <Column dataField="change" dataType="number" width={140} format="#0.####" cellRender={ChangeCell} /> <Column dataField="dayOpen" dataType="number" format="#0.####" /> <Column dataField="dayMin" dataType="number" format="#0.####" /> <Column dataField="dayMax" dataType="number" format="#0.####" /> </DataGrid> )} </div> ); }; export default App;
import { type DataGridTypes } from 'devextreme-react/data-grid'; import React from 'react'; const ChangeCell = (cell: DataGridTypes.ColumnCellTemplateData) => ( <div className={cell.data.change > 0 ? 'inc' : 'dec' }> <span className="current-value">{cell.text}</span> <span className="arrow"></span> <span className="diff">{cell.data.percentChange.toFixed(2)}%</span> </div> ); export default ChangeCell;
import React from 'react'; import type { DataGridTypes } from 'devextreme-react/data-grid'; const PriceCell = (cell: DataGridTypes.ColumnCellTemplateData) => ( <div className={cell.data.change > 0 ? 'inc' : 'dec' }> <span>{cell.text}</span> </div> ); export default PriceCell;
import React from 'react'; import ReactDOM from 'react-dom'; import App from './App.tsx'; ReactDOM.render( <App />, document.getElementById('app'), );
import React from 'react'; const ChangeCell = (cell) => ( <div className={cell.data.change > 0 ? 'inc' : 'dec'}> <span className="current-value">{cell.text}</span> <span className="arrow"></span> <span className="diff">{cell.data.percentChange.toFixed(2)}%</span> </div> ); export default ChangeCell;
import React from 'react'; const PriceCell = (cell) => ( <div className={cell.data.change > 0 ? 'inc' : 'dec'}> <span>{cell.text}</span> </div> ); export default PriceCell;
import React from 'react'; import ReactDOM from 'react-dom'; import App from './App.js'; ReactDOM.render(<App />, document.getElementById('app'));
<!DOCTYPE html> <html lang="en"> <head> <title>DevExtreme Demo</title> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0" /> <link rel="stylesheet" type="text/css" href="https://cdn3.devexpress.com/jslib/26.1.5/css/dx.light.css" /> <link rel="stylesheet" type="text/css" href="./bundle.css" /> </head> <body class="dx-viewport"> <div class="demo-container"> <div id="app"></div> </div> <script src="bundles/vendor/react.vendor.js"></script> <script src="./bundle.js"></script> </body> </html>
#gridContainer span.current-value { display: inline-block; margin-right: 5px; } #gridContainer span.diff { width: 50px; display: inline-block; } #gridContainer .inc { color: var(--dx-color-success); } #gridContainer .dec { color: var(--dx-color-danger); } #gridContainer .inc .arrow, #gridContainer .dec .arrow { display: inline-block; height: 10px; width: 10px; background-repeat: no-repeat; background-size: 10px 10px; } #gridContainer .inc .arrow { background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAADKSURBVHjaYtTaLs1ABEiG0nPRJa56PEHhsxBhmCUQT4OyrwHxcXyKmQgYJgHE64CYDYrXQcXIMhCbAcgWkGzgNKh38QUB0QamIUUErkhKI9ZAGyCeTERkTYaqxWsgKA2txhdG6GGsvUNGGpeBRMUiGhCFGsqGzUBQQJsxkA5AemaiG5hDIBIIgQSgK0FmMDACs549kN5FZLjhA7+A2A2U9YSAOBeLAk4gnoBDczoOcSFGPIUDPxB/wCHHiKtwYGKgMhg1cBAaCBBgAJTUIL3ToPZfAAAAAElFTkSuQmCC'); } #gridContainer .dec .arrow { background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAADJSURBVHjaYvzPgBfgkhYA4o8QFahKmBioDEYNHIQGsgBxIBCLkqgvAYi/g1mMjMjir0EJzR6If/6HpChKMMgMe3DKBeIcKhiY8x/MYoDj+RQYNgdkGLqBbEB8kgzDToL1YjEQhKWB+BUJhj0H64Eahs1AELYhMpJ+gtUiGYbLQBBOI8LANLBaIg1kAAc0vkiAqSPBQFAkHcNi2DGoHMkGgrAENOCRI0ECRQ2JBoKwJTQCfkLZDPgMZPxPXN5NhtJzMSsJVBMAAgwAyWSY2svfmrwAAAAASUVORK5CYII='); }
using System.Collections.Generic; using DevExtreme.MVC.Demos.Models.SignalR; using Microsoft.AspNet.SignalR; namespace DevExtreme.MVC.Demos.Hubs { public class LiveUpdateSignalRHub : Hub { private readonly StockTicker _stockTicker; public LiveUpdateSignalRHub() { _stockTicker = StockTicker.Instance; } public IEnumerable<Stock> GetAllStocks() { return _stockTicker.GetAllStocks(); } } }
using System; using System.Collections.Generic; using System.Threading; using DevExtreme.MVC.Demos.Hubs; using Microsoft.AspNet.SignalR; using Microsoft.AspNet.SignalR.Hubs; using DevExpress.Data.Utils; namespace DevExtreme.MVC.Demos.Models.SignalR { public class StockTicker { public readonly static StockTicker Instance = new StockTicker(GlobalHost.ConnectionManager.GetHubContext<LiveUpdateSignalRHub>().Clients); private readonly IEnumerable<Stock> _stocks; private IHubConnectionContext<dynamic> Clients { get; set; } private readonly TimeSpan _updateInterval = TimeSpan.FromMilliseconds(500); private readonly NonCryptographicRandom _updateOrNotRandom = NonCryptographicRandom.System; private readonly Timer _timer; private readonly object _updateStockPricesLock = new object(); static readonly NonCryptographicRandom random = NonCryptographicRandom.System; private StockTicker(IHubConnectionContext<dynamic> clients) { Clients = clients; _stocks = GenerateStocks(); _timer = new Timer(UpdateStockPrices, null, _updateInterval, _updateInterval); } public IEnumerable<Stock> GetAllStocks() { return _stocks; } static IEnumerable<Stock> GenerateStocks() { return new[] { new Stock(37.95M) { Symbol = "MSFT", DayOpen=36.5M, LastUpdate = DateTime.Now }, new Stock(24.85M) { Symbol = "INTC", DayOpen=24.9M, LastUpdate = DateTime.Now }, new Stock(22.99M){ Symbol = "CSCO", DayOpen=22.7M, LastUpdate = DateTime.Now }, new Stock(30.71M){ Symbol = "SIRI", DayOpen=30.7M, LastUpdate = DateTime.Now }, new Stock(58.73M){ Symbol = "AAPL", DayOpen=54.9M, LastUpdate = DateTime.Now }, new Stock(110M){ Symbol = "HOKU", DayOpen=121.2M, LastUpdate = DateTime.Now }, new Stock(38.11M){ Symbol = "ORCL", DayOpen=37.9M, LastUpdate = DateTime.Now }, new Stock(17.61M) { Symbol = "AMAT", DayOpen=17.5M, LastUpdate = DateTime.Now }, new Stock(40.80M){ Symbol = "YHOO", DayOpen=39.9M, LastUpdate = DateTime.Now }, new Stock(31.85M){ Symbol = "LVLT", DayOpen=32.9M, LastUpdate = DateTime.Now }, new Stock(20.63M){ Symbol = "DELL", DayOpen=17.9M, LastUpdate = DateTime.Now }, new Stock(63.70M) { Symbol = "GOOG", DayOpen=55.9M, LastUpdate = DateTime.Now } }; } private void UpdateStockPrices(object state) { lock(_updateStockPricesLock) { foreach(var stock in _stocks) { if(TryUpdateStockPrice(stock)) { BroadcastStockPrice(stock); } } } } private bool TryUpdateStockPrice(Stock stock) { var r = _updateOrNotRandom.NextDouble(); if(r > .1) { return false; } stock.Update(); return true; } private void BroadcastStockPrice(Stock stock) { Clients.All.updateStockPrice(stock); } } }
using System; using DevExpress.Data.Utils; namespace DevExtreme.MVC.Demos.Models.SignalR { public class Stock { readonly decimal _initPrice; public Stock(decimal price) { Price = price; DayMax = price; DayMin = price; _initPrice = Price; } public string Symbol { get; set; } public decimal Price { get; set; } public decimal DayMax { get; set; } public decimal DayMin { get; set; } public decimal DayOpen { get; set; } public DateTime LastUpdate { get; set; } public decimal Change { get { return Price - DayOpen; } } public double PercentChange { get { return (double)Math.Round(Change * 100 / DayOpen, 2); } } public void Update() { var isNewDay = LastUpdate.Day != DateTime.Now.Day; var change = GenerateChange(); var newPrice = _initPrice + _initPrice * change; Price = newPrice; LastUpdate = DateTime.Now; if(Price > DayMax || isNewDay) DayMax = Price; if(Price < DayMin || isNewDay) DayMin = Price; } static NonCryptographicRandom random = NonCryptographicRandom.System; decimal GenerateChange() { return (decimal)random.Next(-200, 200) / 10000; } } }

For more information about integration with push services, refer to the following help topic: Integration with Push Services.

Data used in this demo is for demonstration purposes only.