locked
SignalR with paging RRS feed

  • Question

  • User-1858683013 posted

    I am new to SignalR and trying to make stock ticker with it but with paging. Each page <g class="gr_ gr_6 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" id="6" data-gr-id="6">have</g> 100 records and I need to update them using SignalR. But <g class="gr_ gr_8 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" id="8" data-gr-id="8">issue</g> is on page 1 it works fine but when I open page 2 then broadcasting values changes for page 1 and page 2 start working fine. How can I keep the lists unique for each page in SignalR?

    <g class="gr_ gr_11 gr-alert gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling ins-del" id="11" data-gr-id="11">Hub</g> Code:

    [HubName("stockTicker")]
    public class StockTickerHub : Hub
    {
        private readonly StockTicker _stockTicker;
    
        public StockTickerHub()
            : this(StockTicker.Instance)
        {
    
        }
    
        public StockTickerHub(StockTicker stockTicker)
        {
            _stockTicker = stockTicker;
        }
    
        public void OpenMarket()
        {
            var page = this.Context.QueryString["page_no"];
            int pageno = Convert.ToInt32(page);
            _stockTicker.OpenMarket(pageno);
        }
    }

    StockTicker Code

    public class StockTicker
    {
        MainModel _model = new MainModel();
        private readonly static Lazy<StockTicker> _instance = new Lazy<StockTicker>(() => new StockTicker(GlobalHost.ConnectionManager.GetHubContext<StockTickerHub>().Clients));
        private readonly TimeSpan _updateInterval = TimeSpan.FromMilliseconds(1000);
    
        private Timer _timer;
        public static volatile int pageno;
    
        private StockTicker(IHubConnectionContext<dynamic> clients)
        {
            Clients = clients;
        }
    
        public static StockTicker Instance
        {
            get
            {
                return _instance.Value;
            }
        }
    
        private IHubConnectionContext<dynamic> Clients
        {
            get;
            set;
        }
    
    
        public void OpenMarket(int page_no)
        {
            pageno = page_no;
            _timer = new Timer(UpdateStockPrices, pageno, _updateInterval, _updateInterval);
        }
    
    
        private void UpdateStockPrices(object state)
        {
    
            var latest_stocks = new List<CoinsViewModel>();
            latest_stocks = _model.Get100PagedAllCurrencies(pageno);
            foreach (var stock in latest_stocks)
            {
                BroadcastStockPrice(stock);
            }
        }
    
        private void BroadcastStockPrice(CoinsViewModel stock)
        {
            Clients.All.updateStockPrice(stock);
        }
    }
    Friday, March 16, 2018 4:00 PM

All replies

  • User61956409 posted

    Hi arbaz1295,

    In your code, I find you call `Clients.All.updateStockPrice(stock)` to push stock data to all connected clients, as you said, when you click/open page 2, the page 1 is also updated with new updates. To solve the problem, in your hub method, you can include a flag (field) that indicates page index of current data, and in your client side code, you can check the current page index of current page and compare it with flag filed to determine if need to update current UI with new data.

    With Regards,

    Fei Han

    Monday, March 19, 2018 6:19 AM
  • User-1858683013 posted

    Hi Fei,

    Thanks for your reply, so far what I found in documentation is that I need to use Groups in order to send the different broadcast to each different page.  my code so far is 

    Hub

    [HubName("stockTicker")]
    public class StockTickerHub : Hub
    {
        private StockTicker _stockTicker;
    
        public StockTickerHub()
            : this(StockTicker.Instance)
        {
    
        }
    
        public StockTickerHub(StockTicker stockTicker)
        {
            _stockTicker = stockTicker;
        }
    
        public void OpenMarket()
        {
            var page = this.Context.QueryString["page_no"];
            int pageno = Convert.ToInt32(page);
            _stockTicker.OpenMarket(pageno);
            tryAddGroup(page);
        }
    
        public Task tryAddGroup(string page)
        {
            return Groups.Add(Context.ConnectionId, page);
        }
    }

    StockTicker.cs

    public class StockTicker
    {
    
        MainModel _model = new MainModel();
        private static Lazy<StockTicker> _instance = new Lazy<StockTicker>(() => new StockTicker(GlobalHost.ConnectionManager.GetHubContext<StockTickerHub>().Clients));
        private TimeSpan _updateInterval = TimeSpan.FromMilliseconds(3000);
    
        private Timer _timer;
        public volatile int pageno;
        private StockTicker(IHubConnectionContext<dynamic> clients)
        {
            Clients = clients;
        }
    
        public static StockTicker Instance
        {
            get
            {
                return _instance.Value;
            }
        }
    
        private IHubConnectionContext<dynamic> Clients
        {
            get;
            set;
        }
    
    
        public void OpenMarket(int page_no)
        {
            pageno = page_no;
            _timer = new Timer(UpdateStockPrices, pageno, _updateInterval, _updateInterval);
        }
    
    
        private void UpdateStockPrices(object state)
        {
    
            var latest_stocks = new List<StockViewModel>();
            latest_stocks = _model.Get100Stocks(pageno);
            foreach (var stock in latest_stocks)
            {
                BroadcastStockPrice(stock, pageno);
            }
        }
        private void BroadcastStockPrice(StockViewModel stock, int pageno)
        {
            Clients.Group(pageno.ToString()).updateStockPrice(stock);
            //Clients.All.updateStockPrice(stock);
        }
    }

    But still on page 2, updates to page 1 stops. I am trying to figure out how to send the different broadcast to different connection ids. Can you please check the code again and suggest me something. Thanks so much.

    Monday, March 19, 2018 6:55 PM
  • User61956409 posted

    Hi arbaz1295,

    It seems that you are using Single-user groups, if it still does not work as expected, please try to debug your code to trace the ConnectionId that you use to add group and send group message, and check if each ConnectionId of each page is added it’s own group.

    Besides, as I suggested in my previous reply, you can try to send flag to clients from your hub method and detect if current UI need to be updated with new data.

    With Regards,

    Fei Han

    Sunday, March 25, 2018 2:29 AM